core.c 245 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467
  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
  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/cgroup.h>
  37. #include <linux/perf_event.h>
  38. #include <linux/trace_events.h>
  39. #include <linux/hw_breakpoint.h>
  40. #include <linux/mm_types.h>
  41. #include <linux/module.h>
  42. #include <linux/mman.h>
  43. #include <linux/compat.h>
  44. #include <linux/bpf.h>
  45. #include <linux/filter.h>
  46. #include <linux/namei.h>
  47. #include <linux/parser.h>
  48. #include "internal.h"
  49. #include <asm/irq_regs.h>
  50. typedef int (*remote_function_f)(void *);
  51. struct remote_function_call {
  52. struct task_struct *p;
  53. remote_function_f func;
  54. void *info;
  55. int ret;
  56. };
  57. static void remote_function(void *data)
  58. {
  59. struct remote_function_call *tfc = data;
  60. struct task_struct *p = tfc->p;
  61. if (p) {
  62. /* -EAGAIN */
  63. if (task_cpu(p) != smp_processor_id())
  64. return;
  65. /*
  66. * Now that we're on right CPU with IRQs disabled, we can test
  67. * if we hit the right task without races.
  68. */
  69. tfc->ret = -ESRCH; /* No such (running) process */
  70. if (p != current)
  71. return;
  72. }
  73. tfc->ret = tfc->func(tfc->info);
  74. }
  75. /**
  76. * task_function_call - call a function on the cpu on which a task runs
  77. * @p: the task to evaluate
  78. * @func: the function to be called
  79. * @info: the function call argument
  80. *
  81. * Calls the function @func when the task is currently running. This might
  82. * be on the current CPU, which just calls the function directly
  83. *
  84. * returns: @func return value, or
  85. * -ESRCH - when the process isn't running
  86. * -EAGAIN - when the process moved away
  87. */
  88. static int
  89. task_function_call(struct task_struct *p, remote_function_f func, void *info)
  90. {
  91. struct remote_function_call data = {
  92. .p = p,
  93. .func = func,
  94. .info = info,
  95. .ret = -EAGAIN,
  96. };
  97. int ret;
  98. do {
  99. ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1);
  100. if (!ret)
  101. ret = data.ret;
  102. } while (ret == -EAGAIN);
  103. return ret;
  104. }
  105. /**
  106. * cpu_function_call - call a function on the cpu
  107. * @func: the function to be called
  108. * @info: the function call argument
  109. *
  110. * Calls the function @func on the remote cpu.
  111. *
  112. * returns: @func return value or -ENXIO when the cpu is offline
  113. */
  114. static int cpu_function_call(int cpu, remote_function_f func, void *info)
  115. {
  116. struct remote_function_call data = {
  117. .p = NULL,
  118. .func = func,
  119. .info = info,
  120. .ret = -ENXIO, /* No such CPU */
  121. };
  122. smp_call_function_single(cpu, remote_function, &data, 1);
  123. return data.ret;
  124. }
  125. static inline struct perf_cpu_context *
  126. __get_cpu_context(struct perf_event_context *ctx)
  127. {
  128. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  129. }
  130. static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
  131. struct perf_event_context *ctx)
  132. {
  133. raw_spin_lock(&cpuctx->ctx.lock);
  134. if (ctx)
  135. raw_spin_lock(&ctx->lock);
  136. }
  137. static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
  138. struct perf_event_context *ctx)
  139. {
  140. if (ctx)
  141. raw_spin_unlock(&ctx->lock);
  142. raw_spin_unlock(&cpuctx->ctx.lock);
  143. }
  144. #define TASK_TOMBSTONE ((void *)-1L)
  145. static bool is_kernel_event(struct perf_event *event)
  146. {
  147. return READ_ONCE(event->owner) == TASK_TOMBSTONE;
  148. }
  149. /*
  150. * On task ctx scheduling...
  151. *
  152. * When !ctx->nr_events a task context will not be scheduled. This means
  153. * we can disable the scheduler hooks (for performance) without leaving
  154. * pending task ctx state.
  155. *
  156. * This however results in two special cases:
  157. *
  158. * - removing the last event from a task ctx; this is relatively straight
  159. * forward and is done in __perf_remove_from_context.
  160. *
  161. * - adding the first event to a task ctx; this is tricky because we cannot
  162. * rely on ctx->is_active and therefore cannot use event_function_call().
  163. * See perf_install_in_context().
  164. *
  165. * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
  166. */
  167. typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
  168. struct perf_event_context *, void *);
  169. struct event_function_struct {
  170. struct perf_event *event;
  171. event_f func;
  172. void *data;
  173. };
  174. static int event_function(void *info)
  175. {
  176. struct event_function_struct *efs = info;
  177. struct perf_event *event = efs->event;
  178. struct perf_event_context *ctx = event->ctx;
  179. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  180. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  181. int ret = 0;
  182. WARN_ON_ONCE(!irqs_disabled());
  183. perf_ctx_lock(cpuctx, task_ctx);
  184. /*
  185. * Since we do the IPI call without holding ctx->lock things can have
  186. * changed, double check we hit the task we set out to hit.
  187. */
  188. if (ctx->task) {
  189. if (ctx->task != current) {
  190. ret = -ESRCH;
  191. goto unlock;
  192. }
  193. /*
  194. * We only use event_function_call() on established contexts,
  195. * and event_function() is only ever called when active (or
  196. * rather, we'll have bailed in task_function_call() or the
  197. * above ctx->task != current test), therefore we must have
  198. * ctx->is_active here.
  199. */
  200. WARN_ON_ONCE(!ctx->is_active);
  201. /*
  202. * And since we have ctx->is_active, cpuctx->task_ctx must
  203. * match.
  204. */
  205. WARN_ON_ONCE(task_ctx != ctx);
  206. } else {
  207. WARN_ON_ONCE(&cpuctx->ctx != ctx);
  208. }
  209. efs->func(event, cpuctx, ctx, efs->data);
  210. unlock:
  211. perf_ctx_unlock(cpuctx, task_ctx);
  212. return ret;
  213. }
  214. static void event_function_local(struct perf_event *event, event_f func, void *data)
  215. {
  216. struct event_function_struct efs = {
  217. .event = event,
  218. .func = func,
  219. .data = data,
  220. };
  221. int ret = event_function(&efs);
  222. WARN_ON_ONCE(ret);
  223. }
  224. static void event_function_call(struct perf_event *event, event_f func, void *data)
  225. {
  226. struct perf_event_context *ctx = event->ctx;
  227. struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
  228. struct event_function_struct efs = {
  229. .event = event,
  230. .func = func,
  231. .data = data,
  232. };
  233. if (!event->parent) {
  234. /*
  235. * If this is a !child event, we must hold ctx::mutex to
  236. * stabilize the the event->ctx relation. See
  237. * perf_event_ctx_lock().
  238. */
  239. lockdep_assert_held(&ctx->mutex);
  240. }
  241. if (!task) {
  242. cpu_function_call(event->cpu, event_function, &efs);
  243. return;
  244. }
  245. if (task == TASK_TOMBSTONE)
  246. return;
  247. again:
  248. if (!task_function_call(task, event_function, &efs))
  249. return;
  250. raw_spin_lock_irq(&ctx->lock);
  251. /*
  252. * Reload the task pointer, it might have been changed by
  253. * a concurrent perf_event_context_sched_out().
  254. */
  255. task = ctx->task;
  256. if (task == TASK_TOMBSTONE) {
  257. raw_spin_unlock_irq(&ctx->lock);
  258. return;
  259. }
  260. if (ctx->is_active) {
  261. raw_spin_unlock_irq(&ctx->lock);
  262. goto again;
  263. }
  264. func(event, NULL, ctx, data);
  265. raw_spin_unlock_irq(&ctx->lock);
  266. }
  267. #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
  268. PERF_FLAG_FD_OUTPUT |\
  269. PERF_FLAG_PID_CGROUP |\
  270. PERF_FLAG_FD_CLOEXEC)
  271. /*
  272. * branch priv levels that need permission checks
  273. */
  274. #define PERF_SAMPLE_BRANCH_PERM_PLM \
  275. (PERF_SAMPLE_BRANCH_KERNEL |\
  276. PERF_SAMPLE_BRANCH_HV)
  277. enum event_type_t {
  278. EVENT_FLEXIBLE = 0x1,
  279. EVENT_PINNED = 0x2,
  280. EVENT_TIME = 0x4,
  281. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  282. };
  283. /*
  284. * perf_sched_events : >0 events exist
  285. * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
  286. */
  287. static void perf_sched_delayed(struct work_struct *work);
  288. DEFINE_STATIC_KEY_FALSE(perf_sched_events);
  289. static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
  290. static DEFINE_MUTEX(perf_sched_mutex);
  291. static atomic_t perf_sched_count;
  292. static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
  293. static DEFINE_PER_CPU(int, perf_sched_cb_usages);
  294. static atomic_t nr_mmap_events __read_mostly;
  295. static atomic_t nr_comm_events __read_mostly;
  296. static atomic_t nr_task_events __read_mostly;
  297. static atomic_t nr_freq_events __read_mostly;
  298. static atomic_t nr_switch_events __read_mostly;
  299. static LIST_HEAD(pmus);
  300. static DEFINE_MUTEX(pmus_lock);
  301. static struct srcu_struct pmus_srcu;
  302. /*
  303. * perf event paranoia level:
  304. * -1 - not paranoid at all
  305. * 0 - disallow raw tracepoint access for unpriv
  306. * 1 - disallow cpu events for unpriv
  307. * 2 - disallow kernel profiling for unpriv
  308. */
  309. int sysctl_perf_event_paranoid __read_mostly = 2;
  310. /* Minimum for 512 kiB + 1 user control page */
  311. int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
  312. /*
  313. * max perf event sample rate
  314. */
  315. #define DEFAULT_MAX_SAMPLE_RATE 100000
  316. #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
  317. #define DEFAULT_CPU_TIME_MAX_PERCENT 25
  318. int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
  319. static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
  320. static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
  321. static int perf_sample_allowed_ns __read_mostly =
  322. DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
  323. static void update_perf_cpu_limits(void)
  324. {
  325. u64 tmp = perf_sample_period_ns;
  326. tmp *= sysctl_perf_cpu_time_max_percent;
  327. tmp = div_u64(tmp, 100);
  328. if (!tmp)
  329. tmp = 1;
  330. WRITE_ONCE(perf_sample_allowed_ns, tmp);
  331. }
  332. static int perf_rotate_context(struct perf_cpu_context *cpuctx);
  333. int perf_proc_update_handler(struct ctl_table *table, int write,
  334. void __user *buffer, size_t *lenp,
  335. loff_t *ppos)
  336. {
  337. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  338. if (ret || !write)
  339. return ret;
  340. max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
  341. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  342. update_perf_cpu_limits();
  343. return 0;
  344. }
  345. int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
  346. int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  347. void __user *buffer, size_t *lenp,
  348. loff_t *ppos)
  349. {
  350. int ret = proc_dointvec(table, write, buffer, lenp, ppos);
  351. if (ret || !write)
  352. return ret;
  353. if (sysctl_perf_cpu_time_max_percent == 100 ||
  354. sysctl_perf_cpu_time_max_percent == 0) {
  355. printk(KERN_WARNING
  356. "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
  357. WRITE_ONCE(perf_sample_allowed_ns, 0);
  358. } else {
  359. update_perf_cpu_limits();
  360. }
  361. return 0;
  362. }
  363. /*
  364. * perf samples are done in some very critical code paths (NMIs).
  365. * If they take too much CPU time, the system can lock up and not
  366. * get any real work done. This will drop the sample rate when
  367. * we detect that events are taking too long.
  368. */
  369. #define NR_ACCUMULATED_SAMPLES 128
  370. static DEFINE_PER_CPU(u64, running_sample_length);
  371. static u64 __report_avg;
  372. static u64 __report_allowed;
  373. static void perf_duration_warn(struct irq_work *w)
  374. {
  375. printk_ratelimited(KERN_WARNING
  376. "perf: interrupt took too long (%lld > %lld), lowering "
  377. "kernel.perf_event_max_sample_rate to %d\n",
  378. __report_avg, __report_allowed,
  379. sysctl_perf_event_sample_rate);
  380. }
  381. static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
  382. void perf_sample_event_took(u64 sample_len_ns)
  383. {
  384. u64 max_len = READ_ONCE(perf_sample_allowed_ns);
  385. u64 running_len;
  386. u64 avg_len;
  387. u32 max;
  388. if (max_len == 0)
  389. return;
  390. /* Decay the counter by 1 average sample. */
  391. running_len = __this_cpu_read(running_sample_length);
  392. running_len -= running_len/NR_ACCUMULATED_SAMPLES;
  393. running_len += sample_len_ns;
  394. __this_cpu_write(running_sample_length, running_len);
  395. /*
  396. * Note: this will be biased artifically low until we have
  397. * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
  398. * from having to maintain a count.
  399. */
  400. avg_len = running_len/NR_ACCUMULATED_SAMPLES;
  401. if (avg_len <= max_len)
  402. return;
  403. __report_avg = avg_len;
  404. __report_allowed = max_len;
  405. /*
  406. * Compute a throttle threshold 25% below the current duration.
  407. */
  408. avg_len += avg_len / 4;
  409. max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
  410. if (avg_len < max)
  411. max /= (u32)avg_len;
  412. else
  413. max = 1;
  414. WRITE_ONCE(perf_sample_allowed_ns, avg_len);
  415. WRITE_ONCE(max_samples_per_tick, max);
  416. sysctl_perf_event_sample_rate = max * HZ;
  417. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  418. if (!irq_work_queue(&perf_duration_work)) {
  419. early_printk("perf: interrupt took too long (%lld > %lld), lowering "
  420. "kernel.perf_event_max_sample_rate to %d\n",
  421. __report_avg, __report_allowed,
  422. sysctl_perf_event_sample_rate);
  423. }
  424. }
  425. static atomic64_t perf_event_id;
  426. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  427. enum event_type_t event_type);
  428. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  429. enum event_type_t event_type,
  430. struct task_struct *task);
  431. static void update_context_time(struct perf_event_context *ctx);
  432. static u64 perf_event_time(struct perf_event *event);
  433. void __weak perf_event_print_debug(void) { }
  434. extern __weak const char *perf_pmu_name(void)
  435. {
  436. return "pmu";
  437. }
  438. static inline u64 perf_clock(void)
  439. {
  440. return local_clock();
  441. }
  442. static inline u64 perf_event_clock(struct perf_event *event)
  443. {
  444. return event->clock();
  445. }
  446. #ifdef CONFIG_CGROUP_PERF
  447. static inline bool
  448. perf_cgroup_match(struct perf_event *event)
  449. {
  450. struct perf_event_context *ctx = event->ctx;
  451. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  452. /* @event doesn't care about cgroup */
  453. if (!event->cgrp)
  454. return true;
  455. /* wants specific cgroup scope but @cpuctx isn't associated with any */
  456. if (!cpuctx->cgrp)
  457. return false;
  458. /*
  459. * Cgroup scoping is recursive. An event enabled for a cgroup is
  460. * also enabled for all its descendant cgroups. If @cpuctx's
  461. * cgroup is a descendant of @event's (the test covers identity
  462. * case), it's a match.
  463. */
  464. return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
  465. event->cgrp->css.cgroup);
  466. }
  467. static inline void perf_detach_cgroup(struct perf_event *event)
  468. {
  469. css_put(&event->cgrp->css);
  470. event->cgrp = NULL;
  471. }
  472. static inline int is_cgroup_event(struct perf_event *event)
  473. {
  474. return event->cgrp != NULL;
  475. }
  476. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  477. {
  478. struct perf_cgroup_info *t;
  479. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  480. return t->time;
  481. }
  482. static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
  483. {
  484. struct perf_cgroup_info *info;
  485. u64 now;
  486. now = perf_clock();
  487. info = this_cpu_ptr(cgrp->info);
  488. info->time += now - info->timestamp;
  489. info->timestamp = now;
  490. }
  491. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  492. {
  493. struct perf_cgroup *cgrp_out = cpuctx->cgrp;
  494. if (cgrp_out)
  495. __update_cgrp_time(cgrp_out);
  496. }
  497. static inline void update_cgrp_time_from_event(struct perf_event *event)
  498. {
  499. struct perf_cgroup *cgrp;
  500. /*
  501. * ensure we access cgroup data only when needed and
  502. * when we know the cgroup is pinned (css_get)
  503. */
  504. if (!is_cgroup_event(event))
  505. return;
  506. cgrp = perf_cgroup_from_task(current, event->ctx);
  507. /*
  508. * Do not update time when cgroup is not active
  509. */
  510. if (cgrp == event->cgrp)
  511. __update_cgrp_time(event->cgrp);
  512. }
  513. static inline void
  514. perf_cgroup_set_timestamp(struct task_struct *task,
  515. struct perf_event_context *ctx)
  516. {
  517. struct perf_cgroup *cgrp;
  518. struct perf_cgroup_info *info;
  519. /*
  520. * ctx->lock held by caller
  521. * ensure we do not access cgroup data
  522. * unless we have the cgroup pinned (css_get)
  523. */
  524. if (!task || !ctx->nr_cgroups)
  525. return;
  526. cgrp = perf_cgroup_from_task(task, ctx);
  527. info = this_cpu_ptr(cgrp->info);
  528. info->timestamp = ctx->timestamp;
  529. }
  530. #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
  531. #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
  532. /*
  533. * reschedule events based on the cgroup constraint of task.
  534. *
  535. * mode SWOUT : schedule out everything
  536. * mode SWIN : schedule in based on cgroup for next
  537. */
  538. static void perf_cgroup_switch(struct task_struct *task, int mode)
  539. {
  540. struct perf_cpu_context *cpuctx;
  541. struct pmu *pmu;
  542. unsigned long flags;
  543. /*
  544. * disable interrupts to avoid geting nr_cgroup
  545. * changes via __perf_event_disable(). Also
  546. * avoids preemption.
  547. */
  548. local_irq_save(flags);
  549. /*
  550. * we reschedule only in the presence of cgroup
  551. * constrained events.
  552. */
  553. list_for_each_entry_rcu(pmu, &pmus, entry) {
  554. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  555. if (cpuctx->unique_pmu != pmu)
  556. continue; /* ensure we process each cpuctx once */
  557. /*
  558. * perf_cgroup_events says at least one
  559. * context on this CPU has cgroup events.
  560. *
  561. * ctx->nr_cgroups reports the number of cgroup
  562. * events for a context.
  563. */
  564. if (cpuctx->ctx.nr_cgroups > 0) {
  565. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  566. perf_pmu_disable(cpuctx->ctx.pmu);
  567. if (mode & PERF_CGROUP_SWOUT) {
  568. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  569. /*
  570. * must not be done before ctxswout due
  571. * to event_filter_match() in event_sched_out()
  572. */
  573. cpuctx->cgrp = NULL;
  574. }
  575. if (mode & PERF_CGROUP_SWIN) {
  576. WARN_ON_ONCE(cpuctx->cgrp);
  577. /*
  578. * set cgrp before ctxsw in to allow
  579. * event_filter_match() to not have to pass
  580. * task around
  581. * we pass the cpuctx->ctx to perf_cgroup_from_task()
  582. * because cgorup events are only per-cpu
  583. */
  584. cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
  585. cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
  586. }
  587. perf_pmu_enable(cpuctx->ctx.pmu);
  588. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  589. }
  590. }
  591. local_irq_restore(flags);
  592. }
  593. static inline void perf_cgroup_sched_out(struct task_struct *task,
  594. struct task_struct *next)
  595. {
  596. struct perf_cgroup *cgrp1;
  597. struct perf_cgroup *cgrp2 = NULL;
  598. rcu_read_lock();
  599. /*
  600. * we come here when we know perf_cgroup_events > 0
  601. * we do not need to pass the ctx here because we know
  602. * we are holding the rcu lock
  603. */
  604. cgrp1 = perf_cgroup_from_task(task, NULL);
  605. cgrp2 = perf_cgroup_from_task(next, NULL);
  606. /*
  607. * only schedule out current cgroup events if we know
  608. * that we are switching to a different cgroup. Otherwise,
  609. * do no touch the cgroup events.
  610. */
  611. if (cgrp1 != cgrp2)
  612. perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
  613. rcu_read_unlock();
  614. }
  615. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  616. struct task_struct *task)
  617. {
  618. struct perf_cgroup *cgrp1;
  619. struct perf_cgroup *cgrp2 = NULL;
  620. rcu_read_lock();
  621. /*
  622. * we come here when we know perf_cgroup_events > 0
  623. * we do not need to pass the ctx here because we know
  624. * we are holding the rcu lock
  625. */
  626. cgrp1 = perf_cgroup_from_task(task, NULL);
  627. cgrp2 = perf_cgroup_from_task(prev, NULL);
  628. /*
  629. * only need to schedule in cgroup events if we are changing
  630. * cgroup during ctxsw. Cgroup events were not scheduled
  631. * out of ctxsw out if that was not the case.
  632. */
  633. if (cgrp1 != cgrp2)
  634. perf_cgroup_switch(task, PERF_CGROUP_SWIN);
  635. rcu_read_unlock();
  636. }
  637. static inline int perf_cgroup_connect(int fd, struct perf_event *event,
  638. struct perf_event_attr *attr,
  639. struct perf_event *group_leader)
  640. {
  641. struct perf_cgroup *cgrp;
  642. struct cgroup_subsys_state *css;
  643. struct fd f = fdget(fd);
  644. int ret = 0;
  645. if (!f.file)
  646. return -EBADF;
  647. css = css_tryget_online_from_dir(f.file->f_path.dentry,
  648. &perf_event_cgrp_subsys);
  649. if (IS_ERR(css)) {
  650. ret = PTR_ERR(css);
  651. goto out;
  652. }
  653. cgrp = container_of(css, struct perf_cgroup, css);
  654. event->cgrp = cgrp;
  655. /*
  656. * all events in a group must monitor
  657. * the same cgroup because a task belongs
  658. * to only one perf cgroup at a time
  659. */
  660. if (group_leader && group_leader->cgrp != cgrp) {
  661. perf_detach_cgroup(event);
  662. ret = -EINVAL;
  663. }
  664. out:
  665. fdput(f);
  666. return ret;
  667. }
  668. static inline void
  669. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  670. {
  671. struct perf_cgroup_info *t;
  672. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  673. event->shadow_ctx_time = now - t->timestamp;
  674. }
  675. static inline void
  676. perf_cgroup_defer_enabled(struct perf_event *event)
  677. {
  678. /*
  679. * when the current task's perf cgroup does not match
  680. * the event's, we need to remember to call the
  681. * perf_mark_enable() function the first time a task with
  682. * a matching perf cgroup is scheduled in.
  683. */
  684. if (is_cgroup_event(event) && !perf_cgroup_match(event))
  685. event->cgrp_defer_enabled = 1;
  686. }
  687. static inline void
  688. perf_cgroup_mark_enabled(struct perf_event *event,
  689. struct perf_event_context *ctx)
  690. {
  691. struct perf_event *sub;
  692. u64 tstamp = perf_event_time(event);
  693. if (!event->cgrp_defer_enabled)
  694. return;
  695. event->cgrp_defer_enabled = 0;
  696. event->tstamp_enabled = tstamp - event->total_time_enabled;
  697. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  698. if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
  699. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  700. sub->cgrp_defer_enabled = 0;
  701. }
  702. }
  703. }
  704. #else /* !CONFIG_CGROUP_PERF */
  705. static inline bool
  706. perf_cgroup_match(struct perf_event *event)
  707. {
  708. return true;
  709. }
  710. static inline void perf_detach_cgroup(struct perf_event *event)
  711. {}
  712. static inline int is_cgroup_event(struct perf_event *event)
  713. {
  714. return 0;
  715. }
  716. static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
  717. {
  718. return 0;
  719. }
  720. static inline void update_cgrp_time_from_event(struct perf_event *event)
  721. {
  722. }
  723. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  724. {
  725. }
  726. static inline void perf_cgroup_sched_out(struct task_struct *task,
  727. struct task_struct *next)
  728. {
  729. }
  730. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  731. struct task_struct *task)
  732. {
  733. }
  734. static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
  735. struct perf_event_attr *attr,
  736. struct perf_event *group_leader)
  737. {
  738. return -EINVAL;
  739. }
  740. static inline void
  741. perf_cgroup_set_timestamp(struct task_struct *task,
  742. struct perf_event_context *ctx)
  743. {
  744. }
  745. void
  746. perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
  747. {
  748. }
  749. static inline void
  750. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  751. {
  752. }
  753. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  754. {
  755. return 0;
  756. }
  757. static inline void
  758. perf_cgroup_defer_enabled(struct perf_event *event)
  759. {
  760. }
  761. static inline void
  762. perf_cgroup_mark_enabled(struct perf_event *event,
  763. struct perf_event_context *ctx)
  764. {
  765. }
  766. #endif
  767. /*
  768. * set default to be dependent on timer tick just
  769. * like original code
  770. */
  771. #define PERF_CPU_HRTIMER (1000 / HZ)
  772. /*
  773. * function must be called with interrupts disbled
  774. */
  775. static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
  776. {
  777. struct perf_cpu_context *cpuctx;
  778. int rotations = 0;
  779. WARN_ON(!irqs_disabled());
  780. cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
  781. rotations = perf_rotate_context(cpuctx);
  782. raw_spin_lock(&cpuctx->hrtimer_lock);
  783. if (rotations)
  784. hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
  785. else
  786. cpuctx->hrtimer_active = 0;
  787. raw_spin_unlock(&cpuctx->hrtimer_lock);
  788. return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
  789. }
  790. static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  791. {
  792. struct hrtimer *timer = &cpuctx->hrtimer;
  793. struct pmu *pmu = cpuctx->ctx.pmu;
  794. u64 interval;
  795. /* no multiplexing needed for SW PMU */
  796. if (pmu->task_ctx_nr == perf_sw_context)
  797. return;
  798. /*
  799. * check default is sane, if not set then force to
  800. * default interval (1/tick)
  801. */
  802. interval = pmu->hrtimer_interval_ms;
  803. if (interval < 1)
  804. interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
  805. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
  806. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  807. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  808. timer->function = perf_mux_hrtimer_handler;
  809. }
  810. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  811. {
  812. struct hrtimer *timer = &cpuctx->hrtimer;
  813. struct pmu *pmu = cpuctx->ctx.pmu;
  814. unsigned long flags;
  815. /* not for SW PMU */
  816. if (pmu->task_ctx_nr == perf_sw_context)
  817. return 0;
  818. raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
  819. if (!cpuctx->hrtimer_active) {
  820. cpuctx->hrtimer_active = 1;
  821. hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
  822. hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
  823. }
  824. raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
  825. return 0;
  826. }
  827. void perf_pmu_disable(struct pmu *pmu)
  828. {
  829. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  830. if (!(*count)++)
  831. pmu->pmu_disable(pmu);
  832. }
  833. void perf_pmu_enable(struct pmu *pmu)
  834. {
  835. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  836. if (!--(*count))
  837. pmu->pmu_enable(pmu);
  838. }
  839. static DEFINE_PER_CPU(struct list_head, active_ctx_list);
  840. /*
  841. * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
  842. * perf_event_task_tick() are fully serialized because they're strictly cpu
  843. * affine and perf_event_ctx{activate,deactivate} are called with IRQs
  844. * disabled, while perf_event_task_tick is called from IRQ context.
  845. */
  846. static void perf_event_ctx_activate(struct perf_event_context *ctx)
  847. {
  848. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  849. WARN_ON(!irqs_disabled());
  850. WARN_ON(!list_empty(&ctx->active_ctx_list));
  851. list_add(&ctx->active_ctx_list, head);
  852. }
  853. static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
  854. {
  855. WARN_ON(!irqs_disabled());
  856. WARN_ON(list_empty(&ctx->active_ctx_list));
  857. list_del_init(&ctx->active_ctx_list);
  858. }
  859. static void get_ctx(struct perf_event_context *ctx)
  860. {
  861. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  862. }
  863. static void free_ctx(struct rcu_head *head)
  864. {
  865. struct perf_event_context *ctx;
  866. ctx = container_of(head, struct perf_event_context, rcu_head);
  867. kfree(ctx->task_ctx_data);
  868. kfree(ctx);
  869. }
  870. static void put_ctx(struct perf_event_context *ctx)
  871. {
  872. if (atomic_dec_and_test(&ctx->refcount)) {
  873. if (ctx->parent_ctx)
  874. put_ctx(ctx->parent_ctx);
  875. if (ctx->task && ctx->task != TASK_TOMBSTONE)
  876. put_task_struct(ctx->task);
  877. call_rcu(&ctx->rcu_head, free_ctx);
  878. }
  879. }
  880. /*
  881. * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
  882. * perf_pmu_migrate_context() we need some magic.
  883. *
  884. * Those places that change perf_event::ctx will hold both
  885. * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
  886. *
  887. * Lock ordering is by mutex address. There are two other sites where
  888. * perf_event_context::mutex nests and those are:
  889. *
  890. * - perf_event_exit_task_context() [ child , 0 ]
  891. * perf_event_exit_event()
  892. * put_event() [ parent, 1 ]
  893. *
  894. * - perf_event_init_context() [ parent, 0 ]
  895. * inherit_task_group()
  896. * inherit_group()
  897. * inherit_event()
  898. * perf_event_alloc()
  899. * perf_init_event()
  900. * perf_try_init_event() [ child , 1 ]
  901. *
  902. * While it appears there is an obvious deadlock here -- the parent and child
  903. * nesting levels are inverted between the two. This is in fact safe because
  904. * life-time rules separate them. That is an exiting task cannot fork, and a
  905. * spawning task cannot (yet) exit.
  906. *
  907. * But remember that that these are parent<->child context relations, and
  908. * migration does not affect children, therefore these two orderings should not
  909. * interact.
  910. *
  911. * The change in perf_event::ctx does not affect children (as claimed above)
  912. * because the sys_perf_event_open() case will install a new event and break
  913. * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
  914. * concerned with cpuctx and that doesn't have children.
  915. *
  916. * The places that change perf_event::ctx will issue:
  917. *
  918. * perf_remove_from_context();
  919. * synchronize_rcu();
  920. * perf_install_in_context();
  921. *
  922. * to affect the change. The remove_from_context() + synchronize_rcu() should
  923. * quiesce the event, after which we can install it in the new location. This
  924. * means that only external vectors (perf_fops, prctl) can perturb the event
  925. * while in transit. Therefore all such accessors should also acquire
  926. * perf_event_context::mutex to serialize against this.
  927. *
  928. * However; because event->ctx can change while we're waiting to acquire
  929. * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
  930. * function.
  931. *
  932. * Lock order:
  933. * cred_guard_mutex
  934. * task_struct::perf_event_mutex
  935. * perf_event_context::mutex
  936. * perf_event::child_mutex;
  937. * perf_event_context::lock
  938. * perf_event::mmap_mutex
  939. * mmap_sem
  940. */
  941. static struct perf_event_context *
  942. perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
  943. {
  944. struct perf_event_context *ctx;
  945. again:
  946. rcu_read_lock();
  947. ctx = ACCESS_ONCE(event->ctx);
  948. if (!atomic_inc_not_zero(&ctx->refcount)) {
  949. rcu_read_unlock();
  950. goto again;
  951. }
  952. rcu_read_unlock();
  953. mutex_lock_nested(&ctx->mutex, nesting);
  954. if (event->ctx != ctx) {
  955. mutex_unlock(&ctx->mutex);
  956. put_ctx(ctx);
  957. goto again;
  958. }
  959. return ctx;
  960. }
  961. static inline struct perf_event_context *
  962. perf_event_ctx_lock(struct perf_event *event)
  963. {
  964. return perf_event_ctx_lock_nested(event, 0);
  965. }
  966. static void perf_event_ctx_unlock(struct perf_event *event,
  967. struct perf_event_context *ctx)
  968. {
  969. mutex_unlock(&ctx->mutex);
  970. put_ctx(ctx);
  971. }
  972. /*
  973. * This must be done under the ctx->lock, such as to serialize against
  974. * context_equiv(), therefore we cannot call put_ctx() since that might end up
  975. * calling scheduler related locks and ctx->lock nests inside those.
  976. */
  977. static __must_check struct perf_event_context *
  978. unclone_ctx(struct perf_event_context *ctx)
  979. {
  980. struct perf_event_context *parent_ctx = ctx->parent_ctx;
  981. lockdep_assert_held(&ctx->lock);
  982. if (parent_ctx)
  983. ctx->parent_ctx = NULL;
  984. ctx->generation++;
  985. return parent_ctx;
  986. }
  987. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  988. {
  989. /*
  990. * only top level events have the pid namespace they were created in
  991. */
  992. if (event->parent)
  993. event = event->parent;
  994. return task_tgid_nr_ns(p, event->ns);
  995. }
  996. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  997. {
  998. /*
  999. * only top level events have the pid namespace they were created in
  1000. */
  1001. if (event->parent)
  1002. event = event->parent;
  1003. return task_pid_nr_ns(p, event->ns);
  1004. }
  1005. /*
  1006. * If we inherit events we want to return the parent event id
  1007. * to userspace.
  1008. */
  1009. static u64 primary_event_id(struct perf_event *event)
  1010. {
  1011. u64 id = event->id;
  1012. if (event->parent)
  1013. id = event->parent->id;
  1014. return id;
  1015. }
  1016. /*
  1017. * Get the perf_event_context for a task and lock it.
  1018. *
  1019. * This has to cope with with the fact that until it is locked,
  1020. * the context could get moved to another task.
  1021. */
  1022. static struct perf_event_context *
  1023. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  1024. {
  1025. struct perf_event_context *ctx;
  1026. retry:
  1027. /*
  1028. * One of the few rules of preemptible RCU is that one cannot do
  1029. * rcu_read_unlock() while holding a scheduler (or nested) lock when
  1030. * part of the read side critical section was irqs-enabled -- see
  1031. * rcu_read_unlock_special().
  1032. *
  1033. * Since ctx->lock nests under rq->lock we must ensure the entire read
  1034. * side critical section has interrupts disabled.
  1035. */
  1036. local_irq_save(*flags);
  1037. rcu_read_lock();
  1038. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  1039. if (ctx) {
  1040. /*
  1041. * If this context is a clone of another, it might
  1042. * get swapped for another underneath us by
  1043. * perf_event_task_sched_out, though the
  1044. * rcu_read_lock() protects us from any context
  1045. * getting freed. Lock the context and check if it
  1046. * got swapped before we could get the lock, and retry
  1047. * if so. If we locked the right context, then it
  1048. * can't get swapped on us any more.
  1049. */
  1050. raw_spin_lock(&ctx->lock);
  1051. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  1052. raw_spin_unlock(&ctx->lock);
  1053. rcu_read_unlock();
  1054. local_irq_restore(*flags);
  1055. goto retry;
  1056. }
  1057. if (ctx->task == TASK_TOMBSTONE ||
  1058. !atomic_inc_not_zero(&ctx->refcount)) {
  1059. raw_spin_unlock(&ctx->lock);
  1060. ctx = NULL;
  1061. } else {
  1062. WARN_ON_ONCE(ctx->task != task);
  1063. }
  1064. }
  1065. rcu_read_unlock();
  1066. if (!ctx)
  1067. local_irq_restore(*flags);
  1068. return ctx;
  1069. }
  1070. /*
  1071. * Get the context for a task and increment its pin_count so it
  1072. * can't get swapped to another task. This also increments its
  1073. * reference count so that the context can't get freed.
  1074. */
  1075. static struct perf_event_context *
  1076. perf_pin_task_context(struct task_struct *task, int ctxn)
  1077. {
  1078. struct perf_event_context *ctx;
  1079. unsigned long flags;
  1080. ctx = perf_lock_task_context(task, ctxn, &flags);
  1081. if (ctx) {
  1082. ++ctx->pin_count;
  1083. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1084. }
  1085. return ctx;
  1086. }
  1087. static void perf_unpin_context(struct perf_event_context *ctx)
  1088. {
  1089. unsigned long flags;
  1090. raw_spin_lock_irqsave(&ctx->lock, flags);
  1091. --ctx->pin_count;
  1092. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1093. }
  1094. /*
  1095. * Update the record of the current time in a context.
  1096. */
  1097. static void update_context_time(struct perf_event_context *ctx)
  1098. {
  1099. u64 now = perf_clock();
  1100. ctx->time += now - ctx->timestamp;
  1101. ctx->timestamp = now;
  1102. }
  1103. static u64 perf_event_time(struct perf_event *event)
  1104. {
  1105. struct perf_event_context *ctx = event->ctx;
  1106. if (is_cgroup_event(event))
  1107. return perf_cgroup_event_time(event);
  1108. return ctx ? ctx->time : 0;
  1109. }
  1110. /*
  1111. * Update the total_time_enabled and total_time_running fields for a event.
  1112. */
  1113. static void update_event_times(struct perf_event *event)
  1114. {
  1115. struct perf_event_context *ctx = event->ctx;
  1116. u64 run_end;
  1117. lockdep_assert_held(&ctx->lock);
  1118. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  1119. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  1120. return;
  1121. /*
  1122. * in cgroup mode, time_enabled represents
  1123. * the time the event was enabled AND active
  1124. * tasks were in the monitored cgroup. This is
  1125. * independent of the activity of the context as
  1126. * there may be a mix of cgroup and non-cgroup events.
  1127. *
  1128. * That is why we treat cgroup events differently
  1129. * here.
  1130. */
  1131. if (is_cgroup_event(event))
  1132. run_end = perf_cgroup_event_time(event);
  1133. else if (ctx->is_active)
  1134. run_end = ctx->time;
  1135. else
  1136. run_end = event->tstamp_stopped;
  1137. event->total_time_enabled = run_end - event->tstamp_enabled;
  1138. if (event->state == PERF_EVENT_STATE_INACTIVE)
  1139. run_end = event->tstamp_stopped;
  1140. else
  1141. run_end = perf_event_time(event);
  1142. event->total_time_running = run_end - event->tstamp_running;
  1143. }
  1144. /*
  1145. * Update total_time_enabled and total_time_running for all events in a group.
  1146. */
  1147. static void update_group_times(struct perf_event *leader)
  1148. {
  1149. struct perf_event *event;
  1150. update_event_times(leader);
  1151. list_for_each_entry(event, &leader->sibling_list, group_entry)
  1152. update_event_times(event);
  1153. }
  1154. static struct list_head *
  1155. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  1156. {
  1157. if (event->attr.pinned)
  1158. return &ctx->pinned_groups;
  1159. else
  1160. return &ctx->flexible_groups;
  1161. }
  1162. /*
  1163. * Add a event from the lists for its context.
  1164. * Must be called with ctx->mutex and ctx->lock held.
  1165. */
  1166. static void
  1167. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  1168. {
  1169. lockdep_assert_held(&ctx->lock);
  1170. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  1171. event->attach_state |= PERF_ATTACH_CONTEXT;
  1172. /*
  1173. * If we're a stand alone event or group leader, we go to the context
  1174. * list, group events are kept attached to the group so that
  1175. * perf_group_detach can, at all times, locate all siblings.
  1176. */
  1177. if (event->group_leader == event) {
  1178. struct list_head *list;
  1179. if (is_software_event(event))
  1180. event->group_flags |= PERF_GROUP_SOFTWARE;
  1181. list = ctx_group_list(event, ctx);
  1182. list_add_tail(&event->group_entry, list);
  1183. }
  1184. if (is_cgroup_event(event))
  1185. ctx->nr_cgroups++;
  1186. list_add_rcu(&event->event_entry, &ctx->event_list);
  1187. ctx->nr_events++;
  1188. if (event->attr.inherit_stat)
  1189. ctx->nr_stat++;
  1190. ctx->generation++;
  1191. }
  1192. /*
  1193. * Initialize event state based on the perf_event_attr::disabled.
  1194. */
  1195. static inline void perf_event__state_init(struct perf_event *event)
  1196. {
  1197. event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
  1198. PERF_EVENT_STATE_INACTIVE;
  1199. }
  1200. static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
  1201. {
  1202. int entry = sizeof(u64); /* value */
  1203. int size = 0;
  1204. int nr = 1;
  1205. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1206. size += sizeof(u64);
  1207. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1208. size += sizeof(u64);
  1209. if (event->attr.read_format & PERF_FORMAT_ID)
  1210. entry += sizeof(u64);
  1211. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1212. nr += nr_siblings;
  1213. size += sizeof(u64);
  1214. }
  1215. size += entry * nr;
  1216. event->read_size = size;
  1217. }
  1218. static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
  1219. {
  1220. struct perf_sample_data *data;
  1221. u16 size = 0;
  1222. if (sample_type & PERF_SAMPLE_IP)
  1223. size += sizeof(data->ip);
  1224. if (sample_type & PERF_SAMPLE_ADDR)
  1225. size += sizeof(data->addr);
  1226. if (sample_type & PERF_SAMPLE_PERIOD)
  1227. size += sizeof(data->period);
  1228. if (sample_type & PERF_SAMPLE_WEIGHT)
  1229. size += sizeof(data->weight);
  1230. if (sample_type & PERF_SAMPLE_READ)
  1231. size += event->read_size;
  1232. if (sample_type & PERF_SAMPLE_DATA_SRC)
  1233. size += sizeof(data->data_src.val);
  1234. if (sample_type & PERF_SAMPLE_TRANSACTION)
  1235. size += sizeof(data->txn);
  1236. event->header_size = size;
  1237. }
  1238. /*
  1239. * Called at perf_event creation and when events are attached/detached from a
  1240. * group.
  1241. */
  1242. static void perf_event__header_size(struct perf_event *event)
  1243. {
  1244. __perf_event_read_size(event,
  1245. event->group_leader->nr_siblings);
  1246. __perf_event_header_size(event, event->attr.sample_type);
  1247. }
  1248. static void perf_event__id_header_size(struct perf_event *event)
  1249. {
  1250. struct perf_sample_data *data;
  1251. u64 sample_type = event->attr.sample_type;
  1252. u16 size = 0;
  1253. if (sample_type & PERF_SAMPLE_TID)
  1254. size += sizeof(data->tid_entry);
  1255. if (sample_type & PERF_SAMPLE_TIME)
  1256. size += sizeof(data->time);
  1257. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  1258. size += sizeof(data->id);
  1259. if (sample_type & PERF_SAMPLE_ID)
  1260. size += sizeof(data->id);
  1261. if (sample_type & PERF_SAMPLE_STREAM_ID)
  1262. size += sizeof(data->stream_id);
  1263. if (sample_type & PERF_SAMPLE_CPU)
  1264. size += sizeof(data->cpu_entry);
  1265. event->id_header_size = size;
  1266. }
  1267. static bool perf_event_validate_size(struct perf_event *event)
  1268. {
  1269. /*
  1270. * The values computed here will be over-written when we actually
  1271. * attach the event.
  1272. */
  1273. __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
  1274. __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
  1275. perf_event__id_header_size(event);
  1276. /*
  1277. * Sum the lot; should not exceed the 64k limit we have on records.
  1278. * Conservative limit to allow for callchains and other variable fields.
  1279. */
  1280. if (event->read_size + event->header_size +
  1281. event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
  1282. return false;
  1283. return true;
  1284. }
  1285. static void perf_group_attach(struct perf_event *event)
  1286. {
  1287. struct perf_event *group_leader = event->group_leader, *pos;
  1288. /*
  1289. * We can have double attach due to group movement in perf_event_open.
  1290. */
  1291. if (event->attach_state & PERF_ATTACH_GROUP)
  1292. return;
  1293. event->attach_state |= PERF_ATTACH_GROUP;
  1294. if (group_leader == event)
  1295. return;
  1296. WARN_ON_ONCE(group_leader->ctx != event->ctx);
  1297. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  1298. !is_software_event(event))
  1299. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  1300. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  1301. group_leader->nr_siblings++;
  1302. perf_event__header_size(group_leader);
  1303. list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
  1304. perf_event__header_size(pos);
  1305. }
  1306. /*
  1307. * Remove a event from the lists for its context.
  1308. * Must be called with ctx->mutex and ctx->lock held.
  1309. */
  1310. static void
  1311. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  1312. {
  1313. struct perf_cpu_context *cpuctx;
  1314. WARN_ON_ONCE(event->ctx != ctx);
  1315. lockdep_assert_held(&ctx->lock);
  1316. /*
  1317. * We can have double detach due to exit/hot-unplug + close.
  1318. */
  1319. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  1320. return;
  1321. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  1322. if (is_cgroup_event(event)) {
  1323. ctx->nr_cgroups--;
  1324. /*
  1325. * Because cgroup events are always per-cpu events, this will
  1326. * always be called from the right CPU.
  1327. */
  1328. cpuctx = __get_cpu_context(ctx);
  1329. /*
  1330. * If there are no more cgroup events then clear cgrp to avoid
  1331. * stale pointer in update_cgrp_time_from_cpuctx().
  1332. */
  1333. if (!ctx->nr_cgroups)
  1334. cpuctx->cgrp = NULL;
  1335. }
  1336. ctx->nr_events--;
  1337. if (event->attr.inherit_stat)
  1338. ctx->nr_stat--;
  1339. list_del_rcu(&event->event_entry);
  1340. if (event->group_leader == event)
  1341. list_del_init(&event->group_entry);
  1342. update_group_times(event);
  1343. /*
  1344. * If event was in error state, then keep it
  1345. * that way, otherwise bogus counts will be
  1346. * returned on read(). The only way to get out
  1347. * of error state is by explicit re-enabling
  1348. * of the event
  1349. */
  1350. if (event->state > PERF_EVENT_STATE_OFF)
  1351. event->state = PERF_EVENT_STATE_OFF;
  1352. ctx->generation++;
  1353. }
  1354. static void perf_group_detach(struct perf_event *event)
  1355. {
  1356. struct perf_event *sibling, *tmp;
  1357. struct list_head *list = NULL;
  1358. /*
  1359. * We can have double detach due to exit/hot-unplug + close.
  1360. */
  1361. if (!(event->attach_state & PERF_ATTACH_GROUP))
  1362. return;
  1363. event->attach_state &= ~PERF_ATTACH_GROUP;
  1364. /*
  1365. * If this is a sibling, remove it from its group.
  1366. */
  1367. if (event->group_leader != event) {
  1368. list_del_init(&event->group_entry);
  1369. event->group_leader->nr_siblings--;
  1370. goto out;
  1371. }
  1372. if (!list_empty(&event->group_entry))
  1373. list = &event->group_entry;
  1374. /*
  1375. * If this was a group event with sibling events then
  1376. * upgrade the siblings to singleton events by adding them
  1377. * to whatever list we are on.
  1378. */
  1379. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  1380. if (list)
  1381. list_move_tail(&sibling->group_entry, list);
  1382. sibling->group_leader = sibling;
  1383. /* Inherit group flags from the previous leader */
  1384. sibling->group_flags = event->group_flags;
  1385. WARN_ON_ONCE(sibling->ctx != event->ctx);
  1386. }
  1387. out:
  1388. perf_event__header_size(event->group_leader);
  1389. list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
  1390. perf_event__header_size(tmp);
  1391. }
  1392. static bool is_orphaned_event(struct perf_event *event)
  1393. {
  1394. return event->state == PERF_EVENT_STATE_DEAD;
  1395. }
  1396. static inline int pmu_filter_match(struct perf_event *event)
  1397. {
  1398. struct pmu *pmu = event->pmu;
  1399. return pmu->filter_match ? pmu->filter_match(event) : 1;
  1400. }
  1401. static inline int
  1402. event_filter_match(struct perf_event *event)
  1403. {
  1404. return (event->cpu == -1 || event->cpu == smp_processor_id())
  1405. && perf_cgroup_match(event) && pmu_filter_match(event);
  1406. }
  1407. static void
  1408. event_sched_out(struct perf_event *event,
  1409. struct perf_cpu_context *cpuctx,
  1410. struct perf_event_context *ctx)
  1411. {
  1412. u64 tstamp = perf_event_time(event);
  1413. u64 delta;
  1414. WARN_ON_ONCE(event->ctx != ctx);
  1415. lockdep_assert_held(&ctx->lock);
  1416. /*
  1417. * An event which could not be activated because of
  1418. * filter mismatch still needs to have its timings
  1419. * maintained, otherwise bogus information is return
  1420. * via read() for time_enabled, time_running:
  1421. */
  1422. if (event->state == PERF_EVENT_STATE_INACTIVE
  1423. && !event_filter_match(event)) {
  1424. delta = tstamp - event->tstamp_stopped;
  1425. event->tstamp_running += delta;
  1426. event->tstamp_stopped = tstamp;
  1427. }
  1428. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1429. return;
  1430. perf_pmu_disable(event->pmu);
  1431. event->tstamp_stopped = tstamp;
  1432. event->pmu->del(event, 0);
  1433. event->oncpu = -1;
  1434. event->state = PERF_EVENT_STATE_INACTIVE;
  1435. if (event->pending_disable) {
  1436. event->pending_disable = 0;
  1437. event->state = PERF_EVENT_STATE_OFF;
  1438. }
  1439. if (!is_software_event(event))
  1440. cpuctx->active_oncpu--;
  1441. if (!--ctx->nr_active)
  1442. perf_event_ctx_deactivate(ctx);
  1443. if (event->attr.freq && event->attr.sample_freq)
  1444. ctx->nr_freq--;
  1445. if (event->attr.exclusive || !cpuctx->active_oncpu)
  1446. cpuctx->exclusive = 0;
  1447. perf_pmu_enable(event->pmu);
  1448. }
  1449. static void
  1450. group_sched_out(struct perf_event *group_event,
  1451. struct perf_cpu_context *cpuctx,
  1452. struct perf_event_context *ctx)
  1453. {
  1454. struct perf_event *event;
  1455. int state = group_event->state;
  1456. event_sched_out(group_event, cpuctx, ctx);
  1457. /*
  1458. * Schedule out siblings (if any):
  1459. */
  1460. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  1461. event_sched_out(event, cpuctx, ctx);
  1462. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  1463. cpuctx->exclusive = 0;
  1464. }
  1465. #define DETACH_GROUP 0x01UL
  1466. /*
  1467. * Cross CPU call to remove a performance event
  1468. *
  1469. * We disable the event on the hardware level first. After that we
  1470. * remove it from the context list.
  1471. */
  1472. static void
  1473. __perf_remove_from_context(struct perf_event *event,
  1474. struct perf_cpu_context *cpuctx,
  1475. struct perf_event_context *ctx,
  1476. void *info)
  1477. {
  1478. unsigned long flags = (unsigned long)info;
  1479. event_sched_out(event, cpuctx, ctx);
  1480. if (flags & DETACH_GROUP)
  1481. perf_group_detach(event);
  1482. list_del_event(event, ctx);
  1483. if (!ctx->nr_events && ctx->is_active) {
  1484. ctx->is_active = 0;
  1485. if (ctx->task) {
  1486. WARN_ON_ONCE(cpuctx->task_ctx != ctx);
  1487. cpuctx->task_ctx = NULL;
  1488. }
  1489. }
  1490. }
  1491. /*
  1492. * Remove the event from a task's (or a CPU's) list of events.
  1493. *
  1494. * If event->ctx is a cloned context, callers must make sure that
  1495. * every task struct that event->ctx->task could possibly point to
  1496. * remains valid. This is OK when called from perf_release since
  1497. * that only calls us on the top-level context, which can't be a clone.
  1498. * When called from perf_event_exit_task, it's OK because the
  1499. * context has been detached from its task.
  1500. */
  1501. static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
  1502. {
  1503. lockdep_assert_held(&event->ctx->mutex);
  1504. event_function_call(event, __perf_remove_from_context, (void *)flags);
  1505. }
  1506. /*
  1507. * Cross CPU call to disable a performance event
  1508. */
  1509. static void __perf_event_disable(struct perf_event *event,
  1510. struct perf_cpu_context *cpuctx,
  1511. struct perf_event_context *ctx,
  1512. void *info)
  1513. {
  1514. if (event->state < PERF_EVENT_STATE_INACTIVE)
  1515. return;
  1516. update_context_time(ctx);
  1517. update_cgrp_time_from_event(event);
  1518. update_group_times(event);
  1519. if (event == event->group_leader)
  1520. group_sched_out(event, cpuctx, ctx);
  1521. else
  1522. event_sched_out(event, cpuctx, ctx);
  1523. event->state = PERF_EVENT_STATE_OFF;
  1524. }
  1525. /*
  1526. * Disable a event.
  1527. *
  1528. * If event->ctx is a cloned context, callers must make sure that
  1529. * every task struct that event->ctx->task could possibly point to
  1530. * remains valid. This condition is satisifed when called through
  1531. * perf_event_for_each_child or perf_event_for_each because they
  1532. * hold the top-level event's child_mutex, so any descendant that
  1533. * goes to exit will block in perf_event_exit_event().
  1534. *
  1535. * When called from perf_pending_event it's OK because event->ctx
  1536. * is the current context on this CPU and preemption is disabled,
  1537. * hence we can't get into perf_event_task_sched_out for this context.
  1538. */
  1539. static void _perf_event_disable(struct perf_event *event)
  1540. {
  1541. struct perf_event_context *ctx = event->ctx;
  1542. raw_spin_lock_irq(&ctx->lock);
  1543. if (event->state <= PERF_EVENT_STATE_OFF) {
  1544. raw_spin_unlock_irq(&ctx->lock);
  1545. return;
  1546. }
  1547. raw_spin_unlock_irq(&ctx->lock);
  1548. event_function_call(event, __perf_event_disable, NULL);
  1549. }
  1550. void perf_event_disable_local(struct perf_event *event)
  1551. {
  1552. event_function_local(event, __perf_event_disable, NULL);
  1553. }
  1554. /*
  1555. * Strictly speaking kernel users cannot create groups and therefore this
  1556. * interface does not need the perf_event_ctx_lock() magic.
  1557. */
  1558. void perf_event_disable(struct perf_event *event)
  1559. {
  1560. struct perf_event_context *ctx;
  1561. ctx = perf_event_ctx_lock(event);
  1562. _perf_event_disable(event);
  1563. perf_event_ctx_unlock(event, ctx);
  1564. }
  1565. EXPORT_SYMBOL_GPL(perf_event_disable);
  1566. static void perf_set_shadow_time(struct perf_event *event,
  1567. struct perf_event_context *ctx,
  1568. u64 tstamp)
  1569. {
  1570. /*
  1571. * use the correct time source for the time snapshot
  1572. *
  1573. * We could get by without this by leveraging the
  1574. * fact that to get to this function, the caller
  1575. * has most likely already called update_context_time()
  1576. * and update_cgrp_time_xx() and thus both timestamp
  1577. * are identical (or very close). Given that tstamp is,
  1578. * already adjusted for cgroup, we could say that:
  1579. * tstamp - ctx->timestamp
  1580. * is equivalent to
  1581. * tstamp - cgrp->timestamp.
  1582. *
  1583. * Then, in perf_output_read(), the calculation would
  1584. * work with no changes because:
  1585. * - event is guaranteed scheduled in
  1586. * - no scheduled out in between
  1587. * - thus the timestamp would be the same
  1588. *
  1589. * But this is a bit hairy.
  1590. *
  1591. * So instead, we have an explicit cgroup call to remain
  1592. * within the time time source all along. We believe it
  1593. * is cleaner and simpler to understand.
  1594. */
  1595. if (is_cgroup_event(event))
  1596. perf_cgroup_set_shadow_time(event, tstamp);
  1597. else
  1598. event->shadow_ctx_time = tstamp - ctx->timestamp;
  1599. }
  1600. #define MAX_INTERRUPTS (~0ULL)
  1601. static void perf_log_throttle(struct perf_event *event, int enable);
  1602. static void perf_log_itrace_start(struct perf_event *event);
  1603. static int
  1604. event_sched_in(struct perf_event *event,
  1605. struct perf_cpu_context *cpuctx,
  1606. struct perf_event_context *ctx)
  1607. {
  1608. u64 tstamp = perf_event_time(event);
  1609. int ret = 0;
  1610. lockdep_assert_held(&ctx->lock);
  1611. if (event->state <= PERF_EVENT_STATE_OFF)
  1612. return 0;
  1613. WRITE_ONCE(event->oncpu, smp_processor_id());
  1614. /*
  1615. * Order event::oncpu write to happen before the ACTIVE state
  1616. * is visible.
  1617. */
  1618. smp_wmb();
  1619. WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
  1620. /*
  1621. * Unthrottle events, since we scheduled we might have missed several
  1622. * ticks already, also for a heavily scheduling task there is little
  1623. * guarantee it'll get a tick in a timely manner.
  1624. */
  1625. if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
  1626. perf_log_throttle(event, 1);
  1627. event->hw.interrupts = 0;
  1628. }
  1629. /*
  1630. * The new state must be visible before we turn it on in the hardware:
  1631. */
  1632. smp_wmb();
  1633. perf_pmu_disable(event->pmu);
  1634. perf_set_shadow_time(event, ctx, tstamp);
  1635. perf_log_itrace_start(event);
  1636. if (event->pmu->add(event, PERF_EF_START)) {
  1637. event->state = PERF_EVENT_STATE_INACTIVE;
  1638. event->oncpu = -1;
  1639. ret = -EAGAIN;
  1640. goto out;
  1641. }
  1642. event->tstamp_running += tstamp - event->tstamp_stopped;
  1643. if (!is_software_event(event))
  1644. cpuctx->active_oncpu++;
  1645. if (!ctx->nr_active++)
  1646. perf_event_ctx_activate(ctx);
  1647. if (event->attr.freq && event->attr.sample_freq)
  1648. ctx->nr_freq++;
  1649. if (event->attr.exclusive)
  1650. cpuctx->exclusive = 1;
  1651. out:
  1652. perf_pmu_enable(event->pmu);
  1653. return ret;
  1654. }
  1655. static int
  1656. group_sched_in(struct perf_event *group_event,
  1657. struct perf_cpu_context *cpuctx,
  1658. struct perf_event_context *ctx)
  1659. {
  1660. struct perf_event *event, *partial_group = NULL;
  1661. struct pmu *pmu = ctx->pmu;
  1662. u64 now = ctx->time;
  1663. bool simulate = false;
  1664. if (group_event->state == PERF_EVENT_STATE_OFF)
  1665. return 0;
  1666. pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
  1667. if (event_sched_in(group_event, cpuctx, ctx)) {
  1668. pmu->cancel_txn(pmu);
  1669. perf_mux_hrtimer_restart(cpuctx);
  1670. return -EAGAIN;
  1671. }
  1672. /*
  1673. * Schedule in siblings as one group (if any):
  1674. */
  1675. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1676. if (event_sched_in(event, cpuctx, ctx)) {
  1677. partial_group = event;
  1678. goto group_error;
  1679. }
  1680. }
  1681. if (!pmu->commit_txn(pmu))
  1682. return 0;
  1683. group_error:
  1684. /*
  1685. * Groups can be scheduled in as one unit only, so undo any
  1686. * partial group before returning:
  1687. * The events up to the failed event are scheduled out normally,
  1688. * tstamp_stopped will be updated.
  1689. *
  1690. * The failed events and the remaining siblings need to have
  1691. * their timings updated as if they had gone thru event_sched_in()
  1692. * and event_sched_out(). This is required to get consistent timings
  1693. * across the group. This also takes care of the case where the group
  1694. * could never be scheduled by ensuring tstamp_stopped is set to mark
  1695. * the time the event was actually stopped, such that time delta
  1696. * calculation in update_event_times() is correct.
  1697. */
  1698. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1699. if (event == partial_group)
  1700. simulate = true;
  1701. if (simulate) {
  1702. event->tstamp_running += now - event->tstamp_stopped;
  1703. event->tstamp_stopped = now;
  1704. } else {
  1705. event_sched_out(event, cpuctx, ctx);
  1706. }
  1707. }
  1708. event_sched_out(group_event, cpuctx, ctx);
  1709. pmu->cancel_txn(pmu);
  1710. perf_mux_hrtimer_restart(cpuctx);
  1711. return -EAGAIN;
  1712. }
  1713. /*
  1714. * Work out whether we can put this event group on the CPU now.
  1715. */
  1716. static int group_can_go_on(struct perf_event *event,
  1717. struct perf_cpu_context *cpuctx,
  1718. int can_add_hw)
  1719. {
  1720. /*
  1721. * Groups consisting entirely of software events can always go on.
  1722. */
  1723. if (event->group_flags & PERF_GROUP_SOFTWARE)
  1724. return 1;
  1725. /*
  1726. * If an exclusive group is already on, no other hardware
  1727. * events can go on.
  1728. */
  1729. if (cpuctx->exclusive)
  1730. return 0;
  1731. /*
  1732. * If this group is exclusive and there are already
  1733. * events on the CPU, it can't go on.
  1734. */
  1735. if (event->attr.exclusive && cpuctx->active_oncpu)
  1736. return 0;
  1737. /*
  1738. * Otherwise, try to add it if all previous groups were able
  1739. * to go on.
  1740. */
  1741. return can_add_hw;
  1742. }
  1743. static void add_event_to_ctx(struct perf_event *event,
  1744. struct perf_event_context *ctx)
  1745. {
  1746. u64 tstamp = perf_event_time(event);
  1747. list_add_event(event, ctx);
  1748. perf_group_attach(event);
  1749. event->tstamp_enabled = tstamp;
  1750. event->tstamp_running = tstamp;
  1751. event->tstamp_stopped = tstamp;
  1752. }
  1753. static void ctx_sched_out(struct perf_event_context *ctx,
  1754. struct perf_cpu_context *cpuctx,
  1755. enum event_type_t event_type);
  1756. static void
  1757. ctx_sched_in(struct perf_event_context *ctx,
  1758. struct perf_cpu_context *cpuctx,
  1759. enum event_type_t event_type,
  1760. struct task_struct *task);
  1761. static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
  1762. struct perf_event_context *ctx)
  1763. {
  1764. if (!cpuctx->task_ctx)
  1765. return;
  1766. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  1767. return;
  1768. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1769. }
  1770. static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
  1771. struct perf_event_context *ctx,
  1772. struct task_struct *task)
  1773. {
  1774. cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
  1775. if (ctx)
  1776. ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
  1777. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
  1778. if (ctx)
  1779. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
  1780. }
  1781. static void ctx_resched(struct perf_cpu_context *cpuctx,
  1782. struct perf_event_context *task_ctx)
  1783. {
  1784. perf_pmu_disable(cpuctx->ctx.pmu);
  1785. if (task_ctx)
  1786. task_ctx_sched_out(cpuctx, task_ctx);
  1787. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  1788. perf_event_sched_in(cpuctx, task_ctx, current);
  1789. perf_pmu_enable(cpuctx->ctx.pmu);
  1790. }
  1791. /*
  1792. * Cross CPU call to install and enable a performance event
  1793. *
  1794. * Very similar to remote_function() + event_function() but cannot assume that
  1795. * things like ctx->is_active and cpuctx->task_ctx are set.
  1796. */
  1797. static int __perf_install_in_context(void *info)
  1798. {
  1799. struct perf_event *event = info;
  1800. struct perf_event_context *ctx = event->ctx;
  1801. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1802. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  1803. bool activate = true;
  1804. int ret = 0;
  1805. raw_spin_lock(&cpuctx->ctx.lock);
  1806. if (ctx->task) {
  1807. raw_spin_lock(&ctx->lock);
  1808. task_ctx = ctx;
  1809. /* If we're on the wrong CPU, try again */
  1810. if (task_cpu(ctx->task) != smp_processor_id()) {
  1811. ret = -ESRCH;
  1812. goto unlock;
  1813. }
  1814. /*
  1815. * If we're on the right CPU, see if the task we target is
  1816. * current, if not we don't have to activate the ctx, a future
  1817. * context switch will do that for us.
  1818. */
  1819. if (ctx->task != current)
  1820. activate = false;
  1821. else
  1822. WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
  1823. } else if (task_ctx) {
  1824. raw_spin_lock(&task_ctx->lock);
  1825. }
  1826. if (activate) {
  1827. ctx_sched_out(ctx, cpuctx, EVENT_TIME);
  1828. add_event_to_ctx(event, ctx);
  1829. ctx_resched(cpuctx, task_ctx);
  1830. } else {
  1831. add_event_to_ctx(event, ctx);
  1832. }
  1833. unlock:
  1834. perf_ctx_unlock(cpuctx, task_ctx);
  1835. return ret;
  1836. }
  1837. /*
  1838. * Attach a performance event to a context.
  1839. *
  1840. * Very similar to event_function_call, see comment there.
  1841. */
  1842. static void
  1843. perf_install_in_context(struct perf_event_context *ctx,
  1844. struct perf_event *event,
  1845. int cpu)
  1846. {
  1847. struct task_struct *task = READ_ONCE(ctx->task);
  1848. lockdep_assert_held(&ctx->mutex);
  1849. event->ctx = ctx;
  1850. if (event->cpu != -1)
  1851. event->cpu = cpu;
  1852. if (!task) {
  1853. cpu_function_call(cpu, __perf_install_in_context, event);
  1854. return;
  1855. }
  1856. /*
  1857. * Should not happen, we validate the ctx is still alive before calling.
  1858. */
  1859. if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
  1860. return;
  1861. /*
  1862. * Installing events is tricky because we cannot rely on ctx->is_active
  1863. * to be set in case this is the nr_events 0 -> 1 transition.
  1864. */
  1865. again:
  1866. /*
  1867. * Cannot use task_function_call() because we need to run on the task's
  1868. * CPU regardless of whether its current or not.
  1869. */
  1870. if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
  1871. return;
  1872. raw_spin_lock_irq(&ctx->lock);
  1873. task = ctx->task;
  1874. if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
  1875. /*
  1876. * Cannot happen because we already checked above (which also
  1877. * cannot happen), and we hold ctx->mutex, which serializes us
  1878. * against perf_event_exit_task_context().
  1879. */
  1880. raw_spin_unlock_irq(&ctx->lock);
  1881. return;
  1882. }
  1883. raw_spin_unlock_irq(&ctx->lock);
  1884. /*
  1885. * Since !ctx->is_active doesn't mean anything, we must IPI
  1886. * unconditionally.
  1887. */
  1888. goto again;
  1889. }
  1890. /*
  1891. * Put a event into inactive state and update time fields.
  1892. * Enabling the leader of a group effectively enables all
  1893. * the group members that aren't explicitly disabled, so we
  1894. * have to update their ->tstamp_enabled also.
  1895. * Note: this works for group members as well as group leaders
  1896. * since the non-leader members' sibling_lists will be empty.
  1897. */
  1898. static void __perf_event_mark_enabled(struct perf_event *event)
  1899. {
  1900. struct perf_event *sub;
  1901. u64 tstamp = perf_event_time(event);
  1902. event->state = PERF_EVENT_STATE_INACTIVE;
  1903. event->tstamp_enabled = tstamp - event->total_time_enabled;
  1904. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  1905. if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  1906. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  1907. }
  1908. }
  1909. /*
  1910. * Cross CPU call to enable a performance event
  1911. */
  1912. static void __perf_event_enable(struct perf_event *event,
  1913. struct perf_cpu_context *cpuctx,
  1914. struct perf_event_context *ctx,
  1915. void *info)
  1916. {
  1917. struct perf_event *leader = event->group_leader;
  1918. struct perf_event_context *task_ctx;
  1919. if (event->state >= PERF_EVENT_STATE_INACTIVE ||
  1920. event->state <= PERF_EVENT_STATE_ERROR)
  1921. return;
  1922. if (ctx->is_active)
  1923. ctx_sched_out(ctx, cpuctx, EVENT_TIME);
  1924. __perf_event_mark_enabled(event);
  1925. if (!ctx->is_active)
  1926. return;
  1927. if (!event_filter_match(event)) {
  1928. if (is_cgroup_event(event))
  1929. perf_cgroup_defer_enabled(event);
  1930. ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
  1931. return;
  1932. }
  1933. /*
  1934. * If the event is in a group and isn't the group leader,
  1935. * then don't put it on unless the group is on.
  1936. */
  1937. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
  1938. ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
  1939. return;
  1940. }
  1941. task_ctx = cpuctx->task_ctx;
  1942. if (ctx->task)
  1943. WARN_ON_ONCE(task_ctx != ctx);
  1944. ctx_resched(cpuctx, task_ctx);
  1945. }
  1946. /*
  1947. * Enable a event.
  1948. *
  1949. * If event->ctx is a cloned context, callers must make sure that
  1950. * every task struct that event->ctx->task could possibly point to
  1951. * remains valid. This condition is satisfied when called through
  1952. * perf_event_for_each_child or perf_event_for_each as described
  1953. * for perf_event_disable.
  1954. */
  1955. static void _perf_event_enable(struct perf_event *event)
  1956. {
  1957. struct perf_event_context *ctx = event->ctx;
  1958. raw_spin_lock_irq(&ctx->lock);
  1959. if (event->state >= PERF_EVENT_STATE_INACTIVE ||
  1960. event->state < PERF_EVENT_STATE_ERROR) {
  1961. raw_spin_unlock_irq(&ctx->lock);
  1962. return;
  1963. }
  1964. /*
  1965. * If the event is in error state, clear that first.
  1966. *
  1967. * That way, if we see the event in error state below, we know that it
  1968. * has gone back into error state, as distinct from the task having
  1969. * been scheduled away before the cross-call arrived.
  1970. */
  1971. if (event->state == PERF_EVENT_STATE_ERROR)
  1972. event->state = PERF_EVENT_STATE_OFF;
  1973. raw_spin_unlock_irq(&ctx->lock);
  1974. event_function_call(event, __perf_event_enable, NULL);
  1975. }
  1976. /*
  1977. * See perf_event_disable();
  1978. */
  1979. void perf_event_enable(struct perf_event *event)
  1980. {
  1981. struct perf_event_context *ctx;
  1982. ctx = perf_event_ctx_lock(event);
  1983. _perf_event_enable(event);
  1984. perf_event_ctx_unlock(event, ctx);
  1985. }
  1986. EXPORT_SYMBOL_GPL(perf_event_enable);
  1987. struct stop_event_data {
  1988. struct perf_event *event;
  1989. unsigned int restart;
  1990. };
  1991. static int __perf_event_stop(void *info)
  1992. {
  1993. struct stop_event_data *sd = info;
  1994. struct perf_event *event = sd->event;
  1995. /* if it's already INACTIVE, do nothing */
  1996. if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
  1997. return 0;
  1998. /* matches smp_wmb() in event_sched_in() */
  1999. smp_rmb();
  2000. /*
  2001. * There is a window with interrupts enabled before we get here,
  2002. * so we need to check again lest we try to stop another CPU's event.
  2003. */
  2004. if (READ_ONCE(event->oncpu) != smp_processor_id())
  2005. return -EAGAIN;
  2006. event->pmu->stop(event, PERF_EF_UPDATE);
  2007. /*
  2008. * May race with the actual stop (through perf_pmu_output_stop()),
  2009. * but it is only used for events with AUX ring buffer, and such
  2010. * events will refuse to restart because of rb::aux_mmap_count==0,
  2011. * see comments in perf_aux_output_begin().
  2012. *
  2013. * Since this is happening on a event-local CPU, no trace is lost
  2014. * while restarting.
  2015. */
  2016. if (sd->restart)
  2017. event->pmu->start(event, PERF_EF_START);
  2018. return 0;
  2019. }
  2020. static int perf_event_restart(struct perf_event *event)
  2021. {
  2022. struct stop_event_data sd = {
  2023. .event = event,
  2024. .restart = 1,
  2025. };
  2026. int ret = 0;
  2027. do {
  2028. if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
  2029. return 0;
  2030. /* matches smp_wmb() in event_sched_in() */
  2031. smp_rmb();
  2032. /*
  2033. * We only want to restart ACTIVE events, so if the event goes
  2034. * inactive here (event->oncpu==-1), there's nothing more to do;
  2035. * fall through with ret==-ENXIO.
  2036. */
  2037. ret = cpu_function_call(READ_ONCE(event->oncpu),
  2038. __perf_event_stop, &sd);
  2039. } while (ret == -EAGAIN);
  2040. return ret;
  2041. }
  2042. /*
  2043. * In order to contain the amount of racy and tricky in the address filter
  2044. * configuration management, it is a two part process:
  2045. *
  2046. * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
  2047. * we update the addresses of corresponding vmas in
  2048. * event::addr_filters_offs array and bump the event::addr_filters_gen;
  2049. * (p2) when an event is scheduled in (pmu::add), it calls
  2050. * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
  2051. * if the generation has changed since the previous call.
  2052. *
  2053. * If (p1) happens while the event is active, we restart it to force (p2).
  2054. *
  2055. * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
  2056. * pre-existing mappings, called once when new filters arrive via SET_FILTER
  2057. * ioctl;
  2058. * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
  2059. * registered mapping, called for every new mmap(), with mm::mmap_sem down
  2060. * for reading;
  2061. * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
  2062. * of exec.
  2063. */
  2064. void perf_event_addr_filters_sync(struct perf_event *event)
  2065. {
  2066. struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
  2067. if (!has_addr_filter(event))
  2068. return;
  2069. raw_spin_lock(&ifh->lock);
  2070. if (event->addr_filters_gen != event->hw.addr_filters_gen) {
  2071. event->pmu->addr_filters_sync(event);
  2072. event->hw.addr_filters_gen = event->addr_filters_gen;
  2073. }
  2074. raw_spin_unlock(&ifh->lock);
  2075. }
  2076. EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
  2077. static int _perf_event_refresh(struct perf_event *event, int refresh)
  2078. {
  2079. /*
  2080. * not supported on inherited events
  2081. */
  2082. if (event->attr.inherit || !is_sampling_event(event))
  2083. return -EINVAL;
  2084. atomic_add(refresh, &event->event_limit);
  2085. _perf_event_enable(event);
  2086. return 0;
  2087. }
  2088. /*
  2089. * See perf_event_disable()
  2090. */
  2091. int perf_event_refresh(struct perf_event *event, int refresh)
  2092. {
  2093. struct perf_event_context *ctx;
  2094. int ret;
  2095. ctx = perf_event_ctx_lock(event);
  2096. ret = _perf_event_refresh(event, refresh);
  2097. perf_event_ctx_unlock(event, ctx);
  2098. return ret;
  2099. }
  2100. EXPORT_SYMBOL_GPL(perf_event_refresh);
  2101. static void ctx_sched_out(struct perf_event_context *ctx,
  2102. struct perf_cpu_context *cpuctx,
  2103. enum event_type_t event_type)
  2104. {
  2105. int is_active = ctx->is_active;
  2106. struct perf_event *event;
  2107. lockdep_assert_held(&ctx->lock);
  2108. if (likely(!ctx->nr_events)) {
  2109. /*
  2110. * See __perf_remove_from_context().
  2111. */
  2112. WARN_ON_ONCE(ctx->is_active);
  2113. if (ctx->task)
  2114. WARN_ON_ONCE(cpuctx->task_ctx);
  2115. return;
  2116. }
  2117. ctx->is_active &= ~event_type;
  2118. if (!(ctx->is_active & EVENT_ALL))
  2119. ctx->is_active = 0;
  2120. if (ctx->task) {
  2121. WARN_ON_ONCE(cpuctx->task_ctx != ctx);
  2122. if (!ctx->is_active)
  2123. cpuctx->task_ctx = NULL;
  2124. }
  2125. /*
  2126. * Always update time if it was set; not only when it changes.
  2127. * Otherwise we can 'forget' to update time for any but the last
  2128. * context we sched out. For example:
  2129. *
  2130. * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
  2131. * ctx_sched_out(.event_type = EVENT_PINNED)
  2132. *
  2133. * would only update time for the pinned events.
  2134. */
  2135. if (is_active & EVENT_TIME) {
  2136. /* update (and stop) ctx time */
  2137. update_context_time(ctx);
  2138. update_cgrp_time_from_cpuctx(cpuctx);
  2139. }
  2140. is_active ^= ctx->is_active; /* changed bits */
  2141. if (!ctx->nr_active || !(is_active & EVENT_ALL))
  2142. return;
  2143. perf_pmu_disable(ctx->pmu);
  2144. if (is_active & EVENT_PINNED) {
  2145. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  2146. group_sched_out(event, cpuctx, ctx);
  2147. }
  2148. if (is_active & EVENT_FLEXIBLE) {
  2149. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  2150. group_sched_out(event, cpuctx, ctx);
  2151. }
  2152. perf_pmu_enable(ctx->pmu);
  2153. }
  2154. /*
  2155. * Test whether two contexts are equivalent, i.e. whether they have both been
  2156. * cloned from the same version of the same context.
  2157. *
  2158. * Equivalence is measured using a generation number in the context that is
  2159. * incremented on each modification to it; see unclone_ctx(), list_add_event()
  2160. * and list_del_event().
  2161. */
  2162. static int context_equiv(struct perf_event_context *ctx1,
  2163. struct perf_event_context *ctx2)
  2164. {
  2165. lockdep_assert_held(&ctx1->lock);
  2166. lockdep_assert_held(&ctx2->lock);
  2167. /* Pinning disables the swap optimization */
  2168. if (ctx1->pin_count || ctx2->pin_count)
  2169. return 0;
  2170. /* If ctx1 is the parent of ctx2 */
  2171. if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
  2172. return 1;
  2173. /* If ctx2 is the parent of ctx1 */
  2174. if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
  2175. return 1;
  2176. /*
  2177. * If ctx1 and ctx2 have the same parent; we flatten the parent
  2178. * hierarchy, see perf_event_init_context().
  2179. */
  2180. if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
  2181. ctx1->parent_gen == ctx2->parent_gen)
  2182. return 1;
  2183. /* Unmatched */
  2184. return 0;
  2185. }
  2186. static void __perf_event_sync_stat(struct perf_event *event,
  2187. struct perf_event *next_event)
  2188. {
  2189. u64 value;
  2190. if (!event->attr.inherit_stat)
  2191. return;
  2192. /*
  2193. * Update the event value, we cannot use perf_event_read()
  2194. * because we're in the middle of a context switch and have IRQs
  2195. * disabled, which upsets smp_call_function_single(), however
  2196. * we know the event must be on the current CPU, therefore we
  2197. * don't need to use it.
  2198. */
  2199. switch (event->state) {
  2200. case PERF_EVENT_STATE_ACTIVE:
  2201. event->pmu->read(event);
  2202. /* fall-through */
  2203. case PERF_EVENT_STATE_INACTIVE:
  2204. update_event_times(event);
  2205. break;
  2206. default:
  2207. break;
  2208. }
  2209. /*
  2210. * In order to keep per-task stats reliable we need to flip the event
  2211. * values when we flip the contexts.
  2212. */
  2213. value = local64_read(&next_event->count);
  2214. value = local64_xchg(&event->count, value);
  2215. local64_set(&next_event->count, value);
  2216. swap(event->total_time_enabled, next_event->total_time_enabled);
  2217. swap(event->total_time_running, next_event->total_time_running);
  2218. /*
  2219. * Since we swizzled the values, update the user visible data too.
  2220. */
  2221. perf_event_update_userpage(event);
  2222. perf_event_update_userpage(next_event);
  2223. }
  2224. static void perf_event_sync_stat(struct perf_event_context *ctx,
  2225. struct perf_event_context *next_ctx)
  2226. {
  2227. struct perf_event *event, *next_event;
  2228. if (!ctx->nr_stat)
  2229. return;
  2230. update_context_time(ctx);
  2231. event = list_first_entry(&ctx->event_list,
  2232. struct perf_event, event_entry);
  2233. next_event = list_first_entry(&next_ctx->event_list,
  2234. struct perf_event, event_entry);
  2235. while (&event->event_entry != &ctx->event_list &&
  2236. &next_event->event_entry != &next_ctx->event_list) {
  2237. __perf_event_sync_stat(event, next_event);
  2238. event = list_next_entry(event, event_entry);
  2239. next_event = list_next_entry(next_event, event_entry);
  2240. }
  2241. }
  2242. static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  2243. struct task_struct *next)
  2244. {
  2245. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  2246. struct perf_event_context *next_ctx;
  2247. struct perf_event_context *parent, *next_parent;
  2248. struct perf_cpu_context *cpuctx;
  2249. int do_switch = 1;
  2250. if (likely(!ctx))
  2251. return;
  2252. cpuctx = __get_cpu_context(ctx);
  2253. if (!cpuctx->task_ctx)
  2254. return;
  2255. rcu_read_lock();
  2256. next_ctx = next->perf_event_ctxp[ctxn];
  2257. if (!next_ctx)
  2258. goto unlock;
  2259. parent = rcu_dereference(ctx->parent_ctx);
  2260. next_parent = rcu_dereference(next_ctx->parent_ctx);
  2261. /* If neither context have a parent context; they cannot be clones. */
  2262. if (!parent && !next_parent)
  2263. goto unlock;
  2264. if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
  2265. /*
  2266. * Looks like the two contexts are clones, so we might be
  2267. * able to optimize the context switch. We lock both
  2268. * contexts and check that they are clones under the
  2269. * lock (including re-checking that neither has been
  2270. * uncloned in the meantime). It doesn't matter which
  2271. * order we take the locks because no other cpu could
  2272. * be trying to lock both of these tasks.
  2273. */
  2274. raw_spin_lock(&ctx->lock);
  2275. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  2276. if (context_equiv(ctx, next_ctx)) {
  2277. WRITE_ONCE(ctx->task, next);
  2278. WRITE_ONCE(next_ctx->task, task);
  2279. swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
  2280. /*
  2281. * RCU_INIT_POINTER here is safe because we've not
  2282. * modified the ctx and the above modification of
  2283. * ctx->task and ctx->task_ctx_data are immaterial
  2284. * since those values are always verified under
  2285. * ctx->lock which we're now holding.
  2286. */
  2287. RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
  2288. RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
  2289. do_switch = 0;
  2290. perf_event_sync_stat(ctx, next_ctx);
  2291. }
  2292. raw_spin_unlock(&next_ctx->lock);
  2293. raw_spin_unlock(&ctx->lock);
  2294. }
  2295. unlock:
  2296. rcu_read_unlock();
  2297. if (do_switch) {
  2298. raw_spin_lock(&ctx->lock);
  2299. task_ctx_sched_out(cpuctx, ctx);
  2300. raw_spin_unlock(&ctx->lock);
  2301. }
  2302. }
  2303. void perf_sched_cb_dec(struct pmu *pmu)
  2304. {
  2305. this_cpu_dec(perf_sched_cb_usages);
  2306. }
  2307. void perf_sched_cb_inc(struct pmu *pmu)
  2308. {
  2309. this_cpu_inc(perf_sched_cb_usages);
  2310. }
  2311. /*
  2312. * This function provides the context switch callback to the lower code
  2313. * layer. It is invoked ONLY when the context switch callback is enabled.
  2314. */
  2315. static void perf_pmu_sched_task(struct task_struct *prev,
  2316. struct task_struct *next,
  2317. bool sched_in)
  2318. {
  2319. struct perf_cpu_context *cpuctx;
  2320. struct pmu *pmu;
  2321. unsigned long flags;
  2322. if (prev == next)
  2323. return;
  2324. local_irq_save(flags);
  2325. rcu_read_lock();
  2326. list_for_each_entry_rcu(pmu, &pmus, entry) {
  2327. if (pmu->sched_task) {
  2328. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  2329. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2330. perf_pmu_disable(pmu);
  2331. pmu->sched_task(cpuctx->task_ctx, sched_in);
  2332. perf_pmu_enable(pmu);
  2333. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2334. }
  2335. }
  2336. rcu_read_unlock();
  2337. local_irq_restore(flags);
  2338. }
  2339. static void perf_event_switch(struct task_struct *task,
  2340. struct task_struct *next_prev, bool sched_in);
  2341. #define for_each_task_context_nr(ctxn) \
  2342. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  2343. /*
  2344. * Called from scheduler to remove the events of the current task,
  2345. * with interrupts disabled.
  2346. *
  2347. * We stop each event and update the event value in event->count.
  2348. *
  2349. * This does not protect us against NMI, but disable()
  2350. * sets the disabled bit in the control field of event _before_
  2351. * accessing the event control register. If a NMI hits, then it will
  2352. * not restart the event.
  2353. */
  2354. void __perf_event_task_sched_out(struct task_struct *task,
  2355. struct task_struct *next)
  2356. {
  2357. int ctxn;
  2358. if (__this_cpu_read(perf_sched_cb_usages))
  2359. perf_pmu_sched_task(task, next, false);
  2360. if (atomic_read(&nr_switch_events))
  2361. perf_event_switch(task, next, false);
  2362. for_each_task_context_nr(ctxn)
  2363. perf_event_context_sched_out(task, ctxn, next);
  2364. /*
  2365. * if cgroup events exist on this CPU, then we need
  2366. * to check if we have to switch out PMU state.
  2367. * cgroup event are system-wide mode only
  2368. */
  2369. if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
  2370. perf_cgroup_sched_out(task, next);
  2371. }
  2372. /*
  2373. * Called with IRQs disabled
  2374. */
  2375. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  2376. enum event_type_t event_type)
  2377. {
  2378. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  2379. }
  2380. static void
  2381. ctx_pinned_sched_in(struct perf_event_context *ctx,
  2382. struct perf_cpu_context *cpuctx)
  2383. {
  2384. struct perf_event *event;
  2385. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  2386. if (event->state <= PERF_EVENT_STATE_OFF)
  2387. continue;
  2388. if (!event_filter_match(event))
  2389. continue;
  2390. /* may need to reset tstamp_enabled */
  2391. if (is_cgroup_event(event))
  2392. perf_cgroup_mark_enabled(event, ctx);
  2393. if (group_can_go_on(event, cpuctx, 1))
  2394. group_sched_in(event, cpuctx, ctx);
  2395. /*
  2396. * If this pinned group hasn't been scheduled,
  2397. * put it in error state.
  2398. */
  2399. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2400. update_group_times(event);
  2401. event->state = PERF_EVENT_STATE_ERROR;
  2402. }
  2403. }
  2404. }
  2405. static void
  2406. ctx_flexible_sched_in(struct perf_event_context *ctx,
  2407. struct perf_cpu_context *cpuctx)
  2408. {
  2409. struct perf_event *event;
  2410. int can_add_hw = 1;
  2411. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  2412. /* Ignore events in OFF or ERROR state */
  2413. if (event->state <= PERF_EVENT_STATE_OFF)
  2414. continue;
  2415. /*
  2416. * Listen to the 'cpu' scheduling filter constraint
  2417. * of events:
  2418. */
  2419. if (!event_filter_match(event))
  2420. continue;
  2421. /* may need to reset tstamp_enabled */
  2422. if (is_cgroup_event(event))
  2423. perf_cgroup_mark_enabled(event, ctx);
  2424. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  2425. if (group_sched_in(event, cpuctx, ctx))
  2426. can_add_hw = 0;
  2427. }
  2428. }
  2429. }
  2430. static void
  2431. ctx_sched_in(struct perf_event_context *ctx,
  2432. struct perf_cpu_context *cpuctx,
  2433. enum event_type_t event_type,
  2434. struct task_struct *task)
  2435. {
  2436. int is_active = ctx->is_active;
  2437. u64 now;
  2438. lockdep_assert_held(&ctx->lock);
  2439. if (likely(!ctx->nr_events))
  2440. return;
  2441. ctx->is_active |= (event_type | EVENT_TIME);
  2442. if (ctx->task) {
  2443. if (!is_active)
  2444. cpuctx->task_ctx = ctx;
  2445. else
  2446. WARN_ON_ONCE(cpuctx->task_ctx != ctx);
  2447. }
  2448. is_active ^= ctx->is_active; /* changed bits */
  2449. if (is_active & EVENT_TIME) {
  2450. /* start ctx time */
  2451. now = perf_clock();
  2452. ctx->timestamp = now;
  2453. perf_cgroup_set_timestamp(task, ctx);
  2454. }
  2455. /*
  2456. * First go through the list and put on any pinned groups
  2457. * in order to give them the best chance of going on.
  2458. */
  2459. if (is_active & EVENT_PINNED)
  2460. ctx_pinned_sched_in(ctx, cpuctx);
  2461. /* Then walk through the lower prio flexible groups */
  2462. if (is_active & EVENT_FLEXIBLE)
  2463. ctx_flexible_sched_in(ctx, cpuctx);
  2464. }
  2465. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  2466. enum event_type_t event_type,
  2467. struct task_struct *task)
  2468. {
  2469. struct perf_event_context *ctx = &cpuctx->ctx;
  2470. ctx_sched_in(ctx, cpuctx, event_type, task);
  2471. }
  2472. static void perf_event_context_sched_in(struct perf_event_context *ctx,
  2473. struct task_struct *task)
  2474. {
  2475. struct perf_cpu_context *cpuctx;
  2476. cpuctx = __get_cpu_context(ctx);
  2477. if (cpuctx->task_ctx == ctx)
  2478. return;
  2479. perf_ctx_lock(cpuctx, ctx);
  2480. perf_pmu_disable(ctx->pmu);
  2481. /*
  2482. * We want to keep the following priority order:
  2483. * cpu pinned (that don't need to move), task pinned,
  2484. * cpu flexible, task flexible.
  2485. */
  2486. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2487. perf_event_sched_in(cpuctx, ctx, task);
  2488. perf_pmu_enable(ctx->pmu);
  2489. perf_ctx_unlock(cpuctx, ctx);
  2490. }
  2491. /*
  2492. * Called from scheduler to add the events of the current task
  2493. * with interrupts disabled.
  2494. *
  2495. * We restore the event value and then enable it.
  2496. *
  2497. * This does not protect us against NMI, but enable()
  2498. * sets the enabled bit in the control field of event _before_
  2499. * accessing the event control register. If a NMI hits, then it will
  2500. * keep the event running.
  2501. */
  2502. void __perf_event_task_sched_in(struct task_struct *prev,
  2503. struct task_struct *task)
  2504. {
  2505. struct perf_event_context *ctx;
  2506. int ctxn;
  2507. /*
  2508. * If cgroup events exist on this CPU, then we need to check if we have
  2509. * to switch in PMU state; cgroup event are system-wide mode only.
  2510. *
  2511. * Since cgroup events are CPU events, we must schedule these in before
  2512. * we schedule in the task events.
  2513. */
  2514. if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
  2515. perf_cgroup_sched_in(prev, task);
  2516. for_each_task_context_nr(ctxn) {
  2517. ctx = task->perf_event_ctxp[ctxn];
  2518. if (likely(!ctx))
  2519. continue;
  2520. perf_event_context_sched_in(ctx, task);
  2521. }
  2522. if (atomic_read(&nr_switch_events))
  2523. perf_event_switch(task, prev, true);
  2524. if (__this_cpu_read(perf_sched_cb_usages))
  2525. perf_pmu_sched_task(prev, task, true);
  2526. }
  2527. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  2528. {
  2529. u64 frequency = event->attr.sample_freq;
  2530. u64 sec = NSEC_PER_SEC;
  2531. u64 divisor, dividend;
  2532. int count_fls, nsec_fls, frequency_fls, sec_fls;
  2533. count_fls = fls64(count);
  2534. nsec_fls = fls64(nsec);
  2535. frequency_fls = fls64(frequency);
  2536. sec_fls = 30;
  2537. /*
  2538. * We got @count in @nsec, with a target of sample_freq HZ
  2539. * the target period becomes:
  2540. *
  2541. * @count * 10^9
  2542. * period = -------------------
  2543. * @nsec * sample_freq
  2544. *
  2545. */
  2546. /*
  2547. * Reduce accuracy by one bit such that @a and @b converge
  2548. * to a similar magnitude.
  2549. */
  2550. #define REDUCE_FLS(a, b) \
  2551. do { \
  2552. if (a##_fls > b##_fls) { \
  2553. a >>= 1; \
  2554. a##_fls--; \
  2555. } else { \
  2556. b >>= 1; \
  2557. b##_fls--; \
  2558. } \
  2559. } while (0)
  2560. /*
  2561. * Reduce accuracy until either term fits in a u64, then proceed with
  2562. * the other, so that finally we can do a u64/u64 division.
  2563. */
  2564. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  2565. REDUCE_FLS(nsec, frequency);
  2566. REDUCE_FLS(sec, count);
  2567. }
  2568. if (count_fls + sec_fls > 64) {
  2569. divisor = nsec * frequency;
  2570. while (count_fls + sec_fls > 64) {
  2571. REDUCE_FLS(count, sec);
  2572. divisor >>= 1;
  2573. }
  2574. dividend = count * sec;
  2575. } else {
  2576. dividend = count * sec;
  2577. while (nsec_fls + frequency_fls > 64) {
  2578. REDUCE_FLS(nsec, frequency);
  2579. dividend >>= 1;
  2580. }
  2581. divisor = nsec * frequency;
  2582. }
  2583. if (!divisor)
  2584. return dividend;
  2585. return div64_u64(dividend, divisor);
  2586. }
  2587. static DEFINE_PER_CPU(int, perf_throttled_count);
  2588. static DEFINE_PER_CPU(u64, perf_throttled_seq);
  2589. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
  2590. {
  2591. struct hw_perf_event *hwc = &event->hw;
  2592. s64 period, sample_period;
  2593. s64 delta;
  2594. period = perf_calculate_period(event, nsec, count);
  2595. delta = (s64)(period - hwc->sample_period);
  2596. delta = (delta + 7) / 8; /* low pass filter */
  2597. sample_period = hwc->sample_period + delta;
  2598. if (!sample_period)
  2599. sample_period = 1;
  2600. hwc->sample_period = sample_period;
  2601. if (local64_read(&hwc->period_left) > 8*sample_period) {
  2602. if (disable)
  2603. event->pmu->stop(event, PERF_EF_UPDATE);
  2604. local64_set(&hwc->period_left, 0);
  2605. if (disable)
  2606. event->pmu->start(event, PERF_EF_RELOAD);
  2607. }
  2608. }
  2609. /*
  2610. * combine freq adjustment with unthrottling to avoid two passes over the
  2611. * events. At the same time, make sure, having freq events does not change
  2612. * the rate of unthrottling as that would introduce bias.
  2613. */
  2614. static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
  2615. int needs_unthr)
  2616. {
  2617. struct perf_event *event;
  2618. struct hw_perf_event *hwc;
  2619. u64 now, period = TICK_NSEC;
  2620. s64 delta;
  2621. /*
  2622. * only need to iterate over all events iff:
  2623. * - context have events in frequency mode (needs freq adjust)
  2624. * - there are events to unthrottle on this cpu
  2625. */
  2626. if (!(ctx->nr_freq || needs_unthr))
  2627. return;
  2628. raw_spin_lock(&ctx->lock);
  2629. perf_pmu_disable(ctx->pmu);
  2630. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2631. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2632. continue;
  2633. if (!event_filter_match(event))
  2634. continue;
  2635. perf_pmu_disable(event->pmu);
  2636. hwc = &event->hw;
  2637. if (hwc->interrupts == MAX_INTERRUPTS) {
  2638. hwc->interrupts = 0;
  2639. perf_log_throttle(event, 1);
  2640. event->pmu->start(event, 0);
  2641. }
  2642. if (!event->attr.freq || !event->attr.sample_freq)
  2643. goto next;
  2644. /*
  2645. * stop the event and update event->count
  2646. */
  2647. event->pmu->stop(event, PERF_EF_UPDATE);
  2648. now = local64_read(&event->count);
  2649. delta = now - hwc->freq_count_stamp;
  2650. hwc->freq_count_stamp = now;
  2651. /*
  2652. * restart the event
  2653. * reload only if value has changed
  2654. * we have stopped the event so tell that
  2655. * to perf_adjust_period() to avoid stopping it
  2656. * twice.
  2657. */
  2658. if (delta > 0)
  2659. perf_adjust_period(event, period, delta, false);
  2660. event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
  2661. next:
  2662. perf_pmu_enable(event->pmu);
  2663. }
  2664. perf_pmu_enable(ctx->pmu);
  2665. raw_spin_unlock(&ctx->lock);
  2666. }
  2667. /*
  2668. * Round-robin a context's events:
  2669. */
  2670. static void rotate_ctx(struct perf_event_context *ctx)
  2671. {
  2672. /*
  2673. * Rotate the first entry last of non-pinned groups. Rotation might be
  2674. * disabled by the inheritance code.
  2675. */
  2676. if (!ctx->rotate_disable)
  2677. list_rotate_left(&ctx->flexible_groups);
  2678. }
  2679. static int perf_rotate_context(struct perf_cpu_context *cpuctx)
  2680. {
  2681. struct perf_event_context *ctx = NULL;
  2682. int rotate = 0;
  2683. if (cpuctx->ctx.nr_events) {
  2684. if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  2685. rotate = 1;
  2686. }
  2687. ctx = cpuctx->task_ctx;
  2688. if (ctx && ctx->nr_events) {
  2689. if (ctx->nr_events != ctx->nr_active)
  2690. rotate = 1;
  2691. }
  2692. if (!rotate)
  2693. goto done;
  2694. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2695. perf_pmu_disable(cpuctx->ctx.pmu);
  2696. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2697. if (ctx)
  2698. ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
  2699. rotate_ctx(&cpuctx->ctx);
  2700. if (ctx)
  2701. rotate_ctx(ctx);
  2702. perf_event_sched_in(cpuctx, ctx, current);
  2703. perf_pmu_enable(cpuctx->ctx.pmu);
  2704. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2705. done:
  2706. return rotate;
  2707. }
  2708. void perf_event_task_tick(void)
  2709. {
  2710. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  2711. struct perf_event_context *ctx, *tmp;
  2712. int throttled;
  2713. WARN_ON(!irqs_disabled());
  2714. __this_cpu_inc(perf_throttled_seq);
  2715. throttled = __this_cpu_xchg(perf_throttled_count, 0);
  2716. tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
  2717. list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
  2718. perf_adjust_freq_unthr_context(ctx, throttled);
  2719. }
  2720. static int event_enable_on_exec(struct perf_event *event,
  2721. struct perf_event_context *ctx)
  2722. {
  2723. if (!event->attr.enable_on_exec)
  2724. return 0;
  2725. event->attr.enable_on_exec = 0;
  2726. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  2727. return 0;
  2728. __perf_event_mark_enabled(event);
  2729. return 1;
  2730. }
  2731. /*
  2732. * Enable all of a task's events that have been marked enable-on-exec.
  2733. * This expects task == current.
  2734. */
  2735. static void perf_event_enable_on_exec(int ctxn)
  2736. {
  2737. struct perf_event_context *ctx, *clone_ctx = NULL;
  2738. struct perf_cpu_context *cpuctx;
  2739. struct perf_event *event;
  2740. unsigned long flags;
  2741. int enabled = 0;
  2742. local_irq_save(flags);
  2743. ctx = current->perf_event_ctxp[ctxn];
  2744. if (!ctx || !ctx->nr_events)
  2745. goto out;
  2746. cpuctx = __get_cpu_context(ctx);
  2747. perf_ctx_lock(cpuctx, ctx);
  2748. ctx_sched_out(ctx, cpuctx, EVENT_TIME);
  2749. list_for_each_entry(event, &ctx->event_list, event_entry)
  2750. enabled |= event_enable_on_exec(event, ctx);
  2751. /*
  2752. * Unclone and reschedule this context if we enabled any event.
  2753. */
  2754. if (enabled) {
  2755. clone_ctx = unclone_ctx(ctx);
  2756. ctx_resched(cpuctx, ctx);
  2757. }
  2758. perf_ctx_unlock(cpuctx, ctx);
  2759. out:
  2760. local_irq_restore(flags);
  2761. if (clone_ctx)
  2762. put_ctx(clone_ctx);
  2763. }
  2764. struct perf_read_data {
  2765. struct perf_event *event;
  2766. bool group;
  2767. int ret;
  2768. };
  2769. /*
  2770. * Cross CPU call to read the hardware event
  2771. */
  2772. static void __perf_event_read(void *info)
  2773. {
  2774. struct perf_read_data *data = info;
  2775. struct perf_event *sub, *event = data->event;
  2776. struct perf_event_context *ctx = event->ctx;
  2777. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  2778. struct pmu *pmu = event->pmu;
  2779. /*
  2780. * If this is a task context, we need to check whether it is
  2781. * the current task context of this cpu. If not it has been
  2782. * scheduled out before the smp call arrived. In that case
  2783. * event->count would have been updated to a recent sample
  2784. * when the event was scheduled out.
  2785. */
  2786. if (ctx->task && cpuctx->task_ctx != ctx)
  2787. return;
  2788. raw_spin_lock(&ctx->lock);
  2789. if (ctx->is_active) {
  2790. update_context_time(ctx);
  2791. update_cgrp_time_from_event(event);
  2792. }
  2793. update_event_times(event);
  2794. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2795. goto unlock;
  2796. if (!data->group) {
  2797. pmu->read(event);
  2798. data->ret = 0;
  2799. goto unlock;
  2800. }
  2801. pmu->start_txn(pmu, PERF_PMU_TXN_READ);
  2802. pmu->read(event);
  2803. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  2804. update_event_times(sub);
  2805. if (sub->state == PERF_EVENT_STATE_ACTIVE) {
  2806. /*
  2807. * Use sibling's PMU rather than @event's since
  2808. * sibling could be on different (eg: software) PMU.
  2809. */
  2810. sub->pmu->read(sub);
  2811. }
  2812. }
  2813. data->ret = pmu->commit_txn(pmu);
  2814. unlock:
  2815. raw_spin_unlock(&ctx->lock);
  2816. }
  2817. static inline u64 perf_event_count(struct perf_event *event)
  2818. {
  2819. if (event->pmu->count)
  2820. return event->pmu->count(event);
  2821. return __perf_event_count(event);
  2822. }
  2823. /*
  2824. * NMI-safe method to read a local event, that is an event that
  2825. * is:
  2826. * - either for the current task, or for this CPU
  2827. * - does not have inherit set, for inherited task events
  2828. * will not be local and we cannot read them atomically
  2829. * - must not have a pmu::count method
  2830. */
  2831. u64 perf_event_read_local(struct perf_event *event)
  2832. {
  2833. unsigned long flags;
  2834. u64 val;
  2835. /*
  2836. * Disabling interrupts avoids all counter scheduling (context
  2837. * switches, timer based rotation and IPIs).
  2838. */
  2839. local_irq_save(flags);
  2840. /* If this is a per-task event, it must be for current */
  2841. WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
  2842. event->hw.target != current);
  2843. /* If this is a per-CPU event, it must be for this CPU */
  2844. WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
  2845. event->cpu != smp_processor_id());
  2846. /*
  2847. * It must not be an event with inherit set, we cannot read
  2848. * all child counters from atomic context.
  2849. */
  2850. WARN_ON_ONCE(event->attr.inherit);
  2851. /*
  2852. * It must not have a pmu::count method, those are not
  2853. * NMI safe.
  2854. */
  2855. WARN_ON_ONCE(event->pmu->count);
  2856. /*
  2857. * If the event is currently on this CPU, its either a per-task event,
  2858. * or local to this CPU. Furthermore it means its ACTIVE (otherwise
  2859. * oncpu == -1).
  2860. */
  2861. if (event->oncpu == smp_processor_id())
  2862. event->pmu->read(event);
  2863. val = local64_read(&event->count);
  2864. local_irq_restore(flags);
  2865. return val;
  2866. }
  2867. static int perf_event_read(struct perf_event *event, bool group)
  2868. {
  2869. int ret = 0;
  2870. /*
  2871. * If event is enabled and currently active on a CPU, update the
  2872. * value in the event structure:
  2873. */
  2874. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  2875. struct perf_read_data data = {
  2876. .event = event,
  2877. .group = group,
  2878. .ret = 0,
  2879. };
  2880. smp_call_function_single(event->oncpu,
  2881. __perf_event_read, &data, 1);
  2882. ret = data.ret;
  2883. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2884. struct perf_event_context *ctx = event->ctx;
  2885. unsigned long flags;
  2886. raw_spin_lock_irqsave(&ctx->lock, flags);
  2887. /*
  2888. * may read while context is not active
  2889. * (e.g., thread is blocked), in that case
  2890. * we cannot update context time
  2891. */
  2892. if (ctx->is_active) {
  2893. update_context_time(ctx);
  2894. update_cgrp_time_from_event(event);
  2895. }
  2896. if (group)
  2897. update_group_times(event);
  2898. else
  2899. update_event_times(event);
  2900. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2901. }
  2902. return ret;
  2903. }
  2904. /*
  2905. * Initialize the perf_event context in a task_struct:
  2906. */
  2907. static void __perf_event_init_context(struct perf_event_context *ctx)
  2908. {
  2909. raw_spin_lock_init(&ctx->lock);
  2910. mutex_init(&ctx->mutex);
  2911. INIT_LIST_HEAD(&ctx->active_ctx_list);
  2912. INIT_LIST_HEAD(&ctx->pinned_groups);
  2913. INIT_LIST_HEAD(&ctx->flexible_groups);
  2914. INIT_LIST_HEAD(&ctx->event_list);
  2915. atomic_set(&ctx->refcount, 1);
  2916. }
  2917. static struct perf_event_context *
  2918. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  2919. {
  2920. struct perf_event_context *ctx;
  2921. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  2922. if (!ctx)
  2923. return NULL;
  2924. __perf_event_init_context(ctx);
  2925. if (task) {
  2926. ctx->task = task;
  2927. get_task_struct(task);
  2928. }
  2929. ctx->pmu = pmu;
  2930. return ctx;
  2931. }
  2932. static struct task_struct *
  2933. find_lively_task_by_vpid(pid_t vpid)
  2934. {
  2935. struct task_struct *task;
  2936. rcu_read_lock();
  2937. if (!vpid)
  2938. task = current;
  2939. else
  2940. task = find_task_by_vpid(vpid);
  2941. if (task)
  2942. get_task_struct(task);
  2943. rcu_read_unlock();
  2944. if (!task)
  2945. return ERR_PTR(-ESRCH);
  2946. return task;
  2947. }
  2948. /*
  2949. * Returns a matching context with refcount and pincount.
  2950. */
  2951. static struct perf_event_context *
  2952. find_get_context(struct pmu *pmu, struct task_struct *task,
  2953. struct perf_event *event)
  2954. {
  2955. struct perf_event_context *ctx, *clone_ctx = NULL;
  2956. struct perf_cpu_context *cpuctx;
  2957. void *task_ctx_data = NULL;
  2958. unsigned long flags;
  2959. int ctxn, err;
  2960. int cpu = event->cpu;
  2961. if (!task) {
  2962. /* Must be root to operate on a CPU event: */
  2963. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  2964. return ERR_PTR(-EACCES);
  2965. /*
  2966. * We could be clever and allow to attach a event to an
  2967. * offline CPU and activate it when the CPU comes up, but
  2968. * that's for later.
  2969. */
  2970. if (!cpu_online(cpu))
  2971. return ERR_PTR(-ENODEV);
  2972. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  2973. ctx = &cpuctx->ctx;
  2974. get_ctx(ctx);
  2975. ++ctx->pin_count;
  2976. return ctx;
  2977. }
  2978. err = -EINVAL;
  2979. ctxn = pmu->task_ctx_nr;
  2980. if (ctxn < 0)
  2981. goto errout;
  2982. if (event->attach_state & PERF_ATTACH_TASK_DATA) {
  2983. task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
  2984. if (!task_ctx_data) {
  2985. err = -ENOMEM;
  2986. goto errout;
  2987. }
  2988. }
  2989. retry:
  2990. ctx = perf_lock_task_context(task, ctxn, &flags);
  2991. if (ctx) {
  2992. clone_ctx = unclone_ctx(ctx);
  2993. ++ctx->pin_count;
  2994. if (task_ctx_data && !ctx->task_ctx_data) {
  2995. ctx->task_ctx_data = task_ctx_data;
  2996. task_ctx_data = NULL;
  2997. }
  2998. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2999. if (clone_ctx)
  3000. put_ctx(clone_ctx);
  3001. } else {
  3002. ctx = alloc_perf_context(pmu, task);
  3003. err = -ENOMEM;
  3004. if (!ctx)
  3005. goto errout;
  3006. if (task_ctx_data) {
  3007. ctx->task_ctx_data = task_ctx_data;
  3008. task_ctx_data = NULL;
  3009. }
  3010. err = 0;
  3011. mutex_lock(&task->perf_event_mutex);
  3012. /*
  3013. * If it has already passed perf_event_exit_task().
  3014. * we must see PF_EXITING, it takes this mutex too.
  3015. */
  3016. if (task->flags & PF_EXITING)
  3017. err = -ESRCH;
  3018. else if (task->perf_event_ctxp[ctxn])
  3019. err = -EAGAIN;
  3020. else {
  3021. get_ctx(ctx);
  3022. ++ctx->pin_count;
  3023. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  3024. }
  3025. mutex_unlock(&task->perf_event_mutex);
  3026. if (unlikely(err)) {
  3027. put_ctx(ctx);
  3028. if (err == -EAGAIN)
  3029. goto retry;
  3030. goto errout;
  3031. }
  3032. }
  3033. kfree(task_ctx_data);
  3034. return ctx;
  3035. errout:
  3036. kfree(task_ctx_data);
  3037. return ERR_PTR(err);
  3038. }
  3039. static void perf_event_free_filter(struct perf_event *event);
  3040. static void perf_event_free_bpf_prog(struct perf_event *event);
  3041. static void free_event_rcu(struct rcu_head *head)
  3042. {
  3043. struct perf_event *event;
  3044. event = container_of(head, struct perf_event, rcu_head);
  3045. if (event->ns)
  3046. put_pid_ns(event->ns);
  3047. perf_event_free_filter(event);
  3048. kfree(event);
  3049. }
  3050. static void ring_buffer_attach(struct perf_event *event,
  3051. struct ring_buffer *rb);
  3052. static void unaccount_event_cpu(struct perf_event *event, int cpu)
  3053. {
  3054. if (event->parent)
  3055. return;
  3056. if (is_cgroup_event(event))
  3057. atomic_dec(&per_cpu(perf_cgroup_events, cpu));
  3058. }
  3059. #ifdef CONFIG_NO_HZ_FULL
  3060. static DEFINE_SPINLOCK(nr_freq_lock);
  3061. #endif
  3062. static void unaccount_freq_event_nohz(void)
  3063. {
  3064. #ifdef CONFIG_NO_HZ_FULL
  3065. spin_lock(&nr_freq_lock);
  3066. if (atomic_dec_and_test(&nr_freq_events))
  3067. tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
  3068. spin_unlock(&nr_freq_lock);
  3069. #endif
  3070. }
  3071. static void unaccount_freq_event(void)
  3072. {
  3073. if (tick_nohz_full_enabled())
  3074. unaccount_freq_event_nohz();
  3075. else
  3076. atomic_dec(&nr_freq_events);
  3077. }
  3078. static void unaccount_event(struct perf_event *event)
  3079. {
  3080. bool dec = false;
  3081. if (event->parent)
  3082. return;
  3083. if (event->attach_state & PERF_ATTACH_TASK)
  3084. dec = true;
  3085. if (event->attr.mmap || event->attr.mmap_data)
  3086. atomic_dec(&nr_mmap_events);
  3087. if (event->attr.comm)
  3088. atomic_dec(&nr_comm_events);
  3089. if (event->attr.task)
  3090. atomic_dec(&nr_task_events);
  3091. if (event->attr.freq)
  3092. unaccount_freq_event();
  3093. if (event->attr.context_switch) {
  3094. dec = true;
  3095. atomic_dec(&nr_switch_events);
  3096. }
  3097. if (is_cgroup_event(event))
  3098. dec = true;
  3099. if (has_branch_stack(event))
  3100. dec = true;
  3101. if (dec) {
  3102. if (!atomic_add_unless(&perf_sched_count, -1, 1))
  3103. schedule_delayed_work(&perf_sched_work, HZ);
  3104. }
  3105. unaccount_event_cpu(event, event->cpu);
  3106. }
  3107. static void perf_sched_delayed(struct work_struct *work)
  3108. {
  3109. mutex_lock(&perf_sched_mutex);
  3110. if (atomic_dec_and_test(&perf_sched_count))
  3111. static_branch_disable(&perf_sched_events);
  3112. mutex_unlock(&perf_sched_mutex);
  3113. }
  3114. /*
  3115. * The following implement mutual exclusion of events on "exclusive" pmus
  3116. * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
  3117. * at a time, so we disallow creating events that might conflict, namely:
  3118. *
  3119. * 1) cpu-wide events in the presence of per-task events,
  3120. * 2) per-task events in the presence of cpu-wide events,
  3121. * 3) two matching events on the same context.
  3122. *
  3123. * The former two cases are handled in the allocation path (perf_event_alloc(),
  3124. * _free_event()), the latter -- before the first perf_install_in_context().
  3125. */
  3126. static int exclusive_event_init(struct perf_event *event)
  3127. {
  3128. struct pmu *pmu = event->pmu;
  3129. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  3130. return 0;
  3131. /*
  3132. * Prevent co-existence of per-task and cpu-wide events on the
  3133. * same exclusive pmu.
  3134. *
  3135. * Negative pmu::exclusive_cnt means there are cpu-wide
  3136. * events on this "exclusive" pmu, positive means there are
  3137. * per-task events.
  3138. *
  3139. * Since this is called in perf_event_alloc() path, event::ctx
  3140. * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
  3141. * to mean "per-task event", because unlike other attach states it
  3142. * never gets cleared.
  3143. */
  3144. if (event->attach_state & PERF_ATTACH_TASK) {
  3145. if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
  3146. return -EBUSY;
  3147. } else {
  3148. if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
  3149. return -EBUSY;
  3150. }
  3151. return 0;
  3152. }
  3153. static void exclusive_event_destroy(struct perf_event *event)
  3154. {
  3155. struct pmu *pmu = event->pmu;
  3156. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  3157. return;
  3158. /* see comment in exclusive_event_init() */
  3159. if (event->attach_state & PERF_ATTACH_TASK)
  3160. atomic_dec(&pmu->exclusive_cnt);
  3161. else
  3162. atomic_inc(&pmu->exclusive_cnt);
  3163. }
  3164. static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
  3165. {
  3166. if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
  3167. (e1->cpu == e2->cpu ||
  3168. e1->cpu == -1 ||
  3169. e2->cpu == -1))
  3170. return true;
  3171. return false;
  3172. }
  3173. /* Called under the same ctx::mutex as perf_install_in_context() */
  3174. static bool exclusive_event_installable(struct perf_event *event,
  3175. struct perf_event_context *ctx)
  3176. {
  3177. struct perf_event *iter_event;
  3178. struct pmu *pmu = event->pmu;
  3179. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  3180. return true;
  3181. list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
  3182. if (exclusive_event_match(iter_event, event))
  3183. return false;
  3184. }
  3185. return true;
  3186. }
  3187. static void perf_addr_filters_splice(struct perf_event *event,
  3188. struct list_head *head);
  3189. static void _free_event(struct perf_event *event)
  3190. {
  3191. irq_work_sync(&event->pending);
  3192. unaccount_event(event);
  3193. if (event->rb) {
  3194. /*
  3195. * Can happen when we close an event with re-directed output.
  3196. *
  3197. * Since we have a 0 refcount, perf_mmap_close() will skip
  3198. * over us; possibly making our ring_buffer_put() the last.
  3199. */
  3200. mutex_lock(&event->mmap_mutex);
  3201. ring_buffer_attach(event, NULL);
  3202. mutex_unlock(&event->mmap_mutex);
  3203. }
  3204. if (is_cgroup_event(event))
  3205. perf_detach_cgroup(event);
  3206. if (!event->parent) {
  3207. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  3208. put_callchain_buffers();
  3209. }
  3210. perf_event_free_bpf_prog(event);
  3211. perf_addr_filters_splice(event, NULL);
  3212. kfree(event->addr_filters_offs);
  3213. if (event->destroy)
  3214. event->destroy(event);
  3215. if (event->ctx)
  3216. put_ctx(event->ctx);
  3217. exclusive_event_destroy(event);
  3218. module_put(event->pmu->module);
  3219. call_rcu(&event->rcu_head, free_event_rcu);
  3220. }
  3221. /*
  3222. * Used to free events which have a known refcount of 1, such as in error paths
  3223. * where the event isn't exposed yet and inherited events.
  3224. */
  3225. static void free_event(struct perf_event *event)
  3226. {
  3227. if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
  3228. "unexpected event refcount: %ld; ptr=%p\n",
  3229. atomic_long_read(&event->refcount), event)) {
  3230. /* leak to avoid use-after-free */
  3231. return;
  3232. }
  3233. _free_event(event);
  3234. }
  3235. /*
  3236. * Remove user event from the owner task.
  3237. */
  3238. static void perf_remove_from_owner(struct perf_event *event)
  3239. {
  3240. struct task_struct *owner;
  3241. rcu_read_lock();
  3242. /*
  3243. * Matches the smp_store_release() in perf_event_exit_task(). If we
  3244. * observe !owner it means the list deletion is complete and we can
  3245. * indeed free this event, otherwise we need to serialize on
  3246. * owner->perf_event_mutex.
  3247. */
  3248. owner = lockless_dereference(event->owner);
  3249. if (owner) {
  3250. /*
  3251. * Since delayed_put_task_struct() also drops the last
  3252. * task reference we can safely take a new reference
  3253. * while holding the rcu_read_lock().
  3254. */
  3255. get_task_struct(owner);
  3256. }
  3257. rcu_read_unlock();
  3258. if (owner) {
  3259. /*
  3260. * If we're here through perf_event_exit_task() we're already
  3261. * holding ctx->mutex which would be an inversion wrt. the
  3262. * normal lock order.
  3263. *
  3264. * However we can safely take this lock because its the child
  3265. * ctx->mutex.
  3266. */
  3267. mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
  3268. /*
  3269. * We have to re-check the event->owner field, if it is cleared
  3270. * we raced with perf_event_exit_task(), acquiring the mutex
  3271. * ensured they're done, and we can proceed with freeing the
  3272. * event.
  3273. */
  3274. if (event->owner) {
  3275. list_del_init(&event->owner_entry);
  3276. smp_store_release(&event->owner, NULL);
  3277. }
  3278. mutex_unlock(&owner->perf_event_mutex);
  3279. put_task_struct(owner);
  3280. }
  3281. }
  3282. static void put_event(struct perf_event *event)
  3283. {
  3284. if (!atomic_long_dec_and_test(&event->refcount))
  3285. return;
  3286. _free_event(event);
  3287. }
  3288. /*
  3289. * Kill an event dead; while event:refcount will preserve the event
  3290. * object, it will not preserve its functionality. Once the last 'user'
  3291. * gives up the object, we'll destroy the thing.
  3292. */
  3293. int perf_event_release_kernel(struct perf_event *event)
  3294. {
  3295. struct perf_event_context *ctx = event->ctx;
  3296. struct perf_event *child, *tmp;
  3297. /*
  3298. * If we got here through err_file: fput(event_file); we will not have
  3299. * attached to a context yet.
  3300. */
  3301. if (!ctx) {
  3302. WARN_ON_ONCE(event->attach_state &
  3303. (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
  3304. goto no_ctx;
  3305. }
  3306. if (!is_kernel_event(event))
  3307. perf_remove_from_owner(event);
  3308. ctx = perf_event_ctx_lock(event);
  3309. WARN_ON_ONCE(ctx->parent_ctx);
  3310. perf_remove_from_context(event, DETACH_GROUP);
  3311. raw_spin_lock_irq(&ctx->lock);
  3312. /*
  3313. * Mark this even as STATE_DEAD, there is no external reference to it
  3314. * anymore.
  3315. *
  3316. * Anybody acquiring event->child_mutex after the below loop _must_
  3317. * also see this, most importantly inherit_event() which will avoid
  3318. * placing more children on the list.
  3319. *
  3320. * Thus this guarantees that we will in fact observe and kill _ALL_
  3321. * child events.
  3322. */
  3323. event->state = PERF_EVENT_STATE_DEAD;
  3324. raw_spin_unlock_irq(&ctx->lock);
  3325. perf_event_ctx_unlock(event, ctx);
  3326. again:
  3327. mutex_lock(&event->child_mutex);
  3328. list_for_each_entry(child, &event->child_list, child_list) {
  3329. /*
  3330. * Cannot change, child events are not migrated, see the
  3331. * comment with perf_event_ctx_lock_nested().
  3332. */
  3333. ctx = lockless_dereference(child->ctx);
  3334. /*
  3335. * Since child_mutex nests inside ctx::mutex, we must jump
  3336. * through hoops. We start by grabbing a reference on the ctx.
  3337. *
  3338. * Since the event cannot get freed while we hold the
  3339. * child_mutex, the context must also exist and have a !0
  3340. * reference count.
  3341. */
  3342. get_ctx(ctx);
  3343. /*
  3344. * Now that we have a ctx ref, we can drop child_mutex, and
  3345. * acquire ctx::mutex without fear of it going away. Then we
  3346. * can re-acquire child_mutex.
  3347. */
  3348. mutex_unlock(&event->child_mutex);
  3349. mutex_lock(&ctx->mutex);
  3350. mutex_lock(&event->child_mutex);
  3351. /*
  3352. * Now that we hold ctx::mutex and child_mutex, revalidate our
  3353. * state, if child is still the first entry, it didn't get freed
  3354. * and we can continue doing so.
  3355. */
  3356. tmp = list_first_entry_or_null(&event->child_list,
  3357. struct perf_event, child_list);
  3358. if (tmp == child) {
  3359. perf_remove_from_context(child, DETACH_GROUP);
  3360. list_del(&child->child_list);
  3361. free_event(child);
  3362. /*
  3363. * This matches the refcount bump in inherit_event();
  3364. * this can't be the last reference.
  3365. */
  3366. put_event(event);
  3367. }
  3368. mutex_unlock(&event->child_mutex);
  3369. mutex_unlock(&ctx->mutex);
  3370. put_ctx(ctx);
  3371. goto again;
  3372. }
  3373. mutex_unlock(&event->child_mutex);
  3374. no_ctx:
  3375. put_event(event); /* Must be the 'last' reference */
  3376. return 0;
  3377. }
  3378. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  3379. /*
  3380. * Called when the last reference to the file is gone.
  3381. */
  3382. static int perf_release(struct inode *inode, struct file *file)
  3383. {
  3384. perf_event_release_kernel(file->private_data);
  3385. return 0;
  3386. }
  3387. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  3388. {
  3389. struct perf_event *child;
  3390. u64 total = 0;
  3391. *enabled = 0;
  3392. *running = 0;
  3393. mutex_lock(&event->child_mutex);
  3394. (void)perf_event_read(event, false);
  3395. total += perf_event_count(event);
  3396. *enabled += event->total_time_enabled +
  3397. atomic64_read(&event->child_total_time_enabled);
  3398. *running += event->total_time_running +
  3399. atomic64_read(&event->child_total_time_running);
  3400. list_for_each_entry(child, &event->child_list, child_list) {
  3401. (void)perf_event_read(child, false);
  3402. total += perf_event_count(child);
  3403. *enabled += child->total_time_enabled;
  3404. *running += child->total_time_running;
  3405. }
  3406. mutex_unlock(&event->child_mutex);
  3407. return total;
  3408. }
  3409. EXPORT_SYMBOL_GPL(perf_event_read_value);
  3410. static int __perf_read_group_add(struct perf_event *leader,
  3411. u64 read_format, u64 *values)
  3412. {
  3413. struct perf_event *sub;
  3414. int n = 1; /* skip @nr */
  3415. int ret;
  3416. ret = perf_event_read(leader, true);
  3417. if (ret)
  3418. return ret;
  3419. /*
  3420. * Since we co-schedule groups, {enabled,running} times of siblings
  3421. * will be identical to those of the leader, so we only publish one
  3422. * set.
  3423. */
  3424. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  3425. values[n++] += leader->total_time_enabled +
  3426. atomic64_read(&leader->child_total_time_enabled);
  3427. }
  3428. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  3429. values[n++] += leader->total_time_running +
  3430. atomic64_read(&leader->child_total_time_running);
  3431. }
  3432. /*
  3433. * Write {count,id} tuples for every sibling.
  3434. */
  3435. values[n++] += perf_event_count(leader);
  3436. if (read_format & PERF_FORMAT_ID)
  3437. values[n++] = primary_event_id(leader);
  3438. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  3439. values[n++] += perf_event_count(sub);
  3440. if (read_format & PERF_FORMAT_ID)
  3441. values[n++] = primary_event_id(sub);
  3442. }
  3443. return 0;
  3444. }
  3445. static int perf_read_group(struct perf_event *event,
  3446. u64 read_format, char __user *buf)
  3447. {
  3448. struct perf_event *leader = event->group_leader, *child;
  3449. struct perf_event_context *ctx = leader->ctx;
  3450. int ret;
  3451. u64 *values;
  3452. lockdep_assert_held(&ctx->mutex);
  3453. values = kzalloc(event->read_size, GFP_KERNEL);
  3454. if (!values)
  3455. return -ENOMEM;
  3456. values[0] = 1 + leader->nr_siblings;
  3457. /*
  3458. * By locking the child_mutex of the leader we effectively
  3459. * lock the child list of all siblings.. XXX explain how.
  3460. */
  3461. mutex_lock(&leader->child_mutex);
  3462. ret = __perf_read_group_add(leader, read_format, values);
  3463. if (ret)
  3464. goto unlock;
  3465. list_for_each_entry(child, &leader->child_list, child_list) {
  3466. ret = __perf_read_group_add(child, read_format, values);
  3467. if (ret)
  3468. goto unlock;
  3469. }
  3470. mutex_unlock(&leader->child_mutex);
  3471. ret = event->read_size;
  3472. if (copy_to_user(buf, values, event->read_size))
  3473. ret = -EFAULT;
  3474. goto out;
  3475. unlock:
  3476. mutex_unlock(&leader->child_mutex);
  3477. out:
  3478. kfree(values);
  3479. return ret;
  3480. }
  3481. static int perf_read_one(struct perf_event *event,
  3482. u64 read_format, char __user *buf)
  3483. {
  3484. u64 enabled, running;
  3485. u64 values[4];
  3486. int n = 0;
  3487. values[n++] = perf_event_read_value(event, &enabled, &running);
  3488. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3489. values[n++] = enabled;
  3490. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3491. values[n++] = running;
  3492. if (read_format & PERF_FORMAT_ID)
  3493. values[n++] = primary_event_id(event);
  3494. if (copy_to_user(buf, values, n * sizeof(u64)))
  3495. return -EFAULT;
  3496. return n * sizeof(u64);
  3497. }
  3498. static bool is_event_hup(struct perf_event *event)
  3499. {
  3500. bool no_children;
  3501. if (event->state > PERF_EVENT_STATE_EXIT)
  3502. return false;
  3503. mutex_lock(&event->child_mutex);
  3504. no_children = list_empty(&event->child_list);
  3505. mutex_unlock(&event->child_mutex);
  3506. return no_children;
  3507. }
  3508. /*
  3509. * Read the performance event - simple non blocking version for now
  3510. */
  3511. static ssize_t
  3512. __perf_read(struct perf_event *event, char __user *buf, size_t count)
  3513. {
  3514. u64 read_format = event->attr.read_format;
  3515. int ret;
  3516. /*
  3517. * Return end-of-file for a read on a event that is in
  3518. * error state (i.e. because it was pinned but it couldn't be
  3519. * scheduled on to the CPU at some point).
  3520. */
  3521. if (event->state == PERF_EVENT_STATE_ERROR)
  3522. return 0;
  3523. if (count < event->read_size)
  3524. return -ENOSPC;
  3525. WARN_ON_ONCE(event->ctx->parent_ctx);
  3526. if (read_format & PERF_FORMAT_GROUP)
  3527. ret = perf_read_group(event, read_format, buf);
  3528. else
  3529. ret = perf_read_one(event, read_format, buf);
  3530. return ret;
  3531. }
  3532. static ssize_t
  3533. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  3534. {
  3535. struct perf_event *event = file->private_data;
  3536. struct perf_event_context *ctx;
  3537. int ret;
  3538. ctx = perf_event_ctx_lock(event);
  3539. ret = __perf_read(event, buf, count);
  3540. perf_event_ctx_unlock(event, ctx);
  3541. return ret;
  3542. }
  3543. static unsigned int perf_poll(struct file *file, poll_table *wait)
  3544. {
  3545. struct perf_event *event = file->private_data;
  3546. struct ring_buffer *rb;
  3547. unsigned int events = POLLHUP;
  3548. poll_wait(file, &event->waitq, wait);
  3549. if (is_event_hup(event))
  3550. return events;
  3551. /*
  3552. * Pin the event->rb by taking event->mmap_mutex; otherwise
  3553. * perf_event_set_output() can swizzle our rb and make us miss wakeups.
  3554. */
  3555. mutex_lock(&event->mmap_mutex);
  3556. rb = event->rb;
  3557. if (rb)
  3558. events = atomic_xchg(&rb->poll, 0);
  3559. mutex_unlock(&event->mmap_mutex);
  3560. return events;
  3561. }
  3562. static void _perf_event_reset(struct perf_event *event)
  3563. {
  3564. (void)perf_event_read(event, false);
  3565. local64_set(&event->count, 0);
  3566. perf_event_update_userpage(event);
  3567. }
  3568. /*
  3569. * Holding the top-level event's child_mutex means that any
  3570. * descendant process that has inherited this event will block
  3571. * in perf_event_exit_event() if it goes to exit, thus satisfying the
  3572. * task existence requirements of perf_event_enable/disable.
  3573. */
  3574. static void perf_event_for_each_child(struct perf_event *event,
  3575. void (*func)(struct perf_event *))
  3576. {
  3577. struct perf_event *child;
  3578. WARN_ON_ONCE(event->ctx->parent_ctx);
  3579. mutex_lock(&event->child_mutex);
  3580. func(event);
  3581. list_for_each_entry(child, &event->child_list, child_list)
  3582. func(child);
  3583. mutex_unlock(&event->child_mutex);
  3584. }
  3585. static void perf_event_for_each(struct perf_event *event,
  3586. void (*func)(struct perf_event *))
  3587. {
  3588. struct perf_event_context *ctx = event->ctx;
  3589. struct perf_event *sibling;
  3590. lockdep_assert_held(&ctx->mutex);
  3591. event = event->group_leader;
  3592. perf_event_for_each_child(event, func);
  3593. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  3594. perf_event_for_each_child(sibling, func);
  3595. }
  3596. static void __perf_event_period(struct perf_event *event,
  3597. struct perf_cpu_context *cpuctx,
  3598. struct perf_event_context *ctx,
  3599. void *info)
  3600. {
  3601. u64 value = *((u64 *)info);
  3602. bool active;
  3603. if (event->attr.freq) {
  3604. event->attr.sample_freq = value;
  3605. } else {
  3606. event->attr.sample_period = value;
  3607. event->hw.sample_period = value;
  3608. }
  3609. active = (event->state == PERF_EVENT_STATE_ACTIVE);
  3610. if (active) {
  3611. perf_pmu_disable(ctx->pmu);
  3612. /*
  3613. * We could be throttled; unthrottle now to avoid the tick
  3614. * trying to unthrottle while we already re-started the event.
  3615. */
  3616. if (event->hw.interrupts == MAX_INTERRUPTS) {
  3617. event->hw.interrupts = 0;
  3618. perf_log_throttle(event, 1);
  3619. }
  3620. event->pmu->stop(event, PERF_EF_UPDATE);
  3621. }
  3622. local64_set(&event->hw.period_left, 0);
  3623. if (active) {
  3624. event->pmu->start(event, PERF_EF_RELOAD);
  3625. perf_pmu_enable(ctx->pmu);
  3626. }
  3627. }
  3628. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  3629. {
  3630. u64 value;
  3631. if (!is_sampling_event(event))
  3632. return -EINVAL;
  3633. if (copy_from_user(&value, arg, sizeof(value)))
  3634. return -EFAULT;
  3635. if (!value)
  3636. return -EINVAL;
  3637. if (event->attr.freq && value > sysctl_perf_event_sample_rate)
  3638. return -EINVAL;
  3639. event_function_call(event, __perf_event_period, &value);
  3640. return 0;
  3641. }
  3642. static const struct file_operations perf_fops;
  3643. static inline int perf_fget_light(int fd, struct fd *p)
  3644. {
  3645. struct fd f = fdget(fd);
  3646. if (!f.file)
  3647. return -EBADF;
  3648. if (f.file->f_op != &perf_fops) {
  3649. fdput(f);
  3650. return -EBADF;
  3651. }
  3652. *p = f;
  3653. return 0;
  3654. }
  3655. static int perf_event_set_output(struct perf_event *event,
  3656. struct perf_event *output_event);
  3657. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  3658. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
  3659. static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
  3660. {
  3661. void (*func)(struct perf_event *);
  3662. u32 flags = arg;
  3663. switch (cmd) {
  3664. case PERF_EVENT_IOC_ENABLE:
  3665. func = _perf_event_enable;
  3666. break;
  3667. case PERF_EVENT_IOC_DISABLE:
  3668. func = _perf_event_disable;
  3669. break;
  3670. case PERF_EVENT_IOC_RESET:
  3671. func = _perf_event_reset;
  3672. break;
  3673. case PERF_EVENT_IOC_REFRESH:
  3674. return _perf_event_refresh(event, arg);
  3675. case PERF_EVENT_IOC_PERIOD:
  3676. return perf_event_period(event, (u64 __user *)arg);
  3677. case PERF_EVENT_IOC_ID:
  3678. {
  3679. u64 id = primary_event_id(event);
  3680. if (copy_to_user((void __user *)arg, &id, sizeof(id)))
  3681. return -EFAULT;
  3682. return 0;
  3683. }
  3684. case PERF_EVENT_IOC_SET_OUTPUT:
  3685. {
  3686. int ret;
  3687. if (arg != -1) {
  3688. struct perf_event *output_event;
  3689. struct fd output;
  3690. ret = perf_fget_light(arg, &output);
  3691. if (ret)
  3692. return ret;
  3693. output_event = output.file->private_data;
  3694. ret = perf_event_set_output(event, output_event);
  3695. fdput(output);
  3696. } else {
  3697. ret = perf_event_set_output(event, NULL);
  3698. }
  3699. return ret;
  3700. }
  3701. case PERF_EVENT_IOC_SET_FILTER:
  3702. return perf_event_set_filter(event, (void __user *)arg);
  3703. case PERF_EVENT_IOC_SET_BPF:
  3704. return perf_event_set_bpf_prog(event, arg);
  3705. case PERF_EVENT_IOC_PAUSE_OUTPUT: {
  3706. struct ring_buffer *rb;
  3707. rcu_read_lock();
  3708. rb = rcu_dereference(event->rb);
  3709. if (!rb || !rb->nr_pages) {
  3710. rcu_read_unlock();
  3711. return -EINVAL;
  3712. }
  3713. rb_toggle_paused(rb, !!arg);
  3714. rcu_read_unlock();
  3715. return 0;
  3716. }
  3717. default:
  3718. return -ENOTTY;
  3719. }
  3720. if (flags & PERF_IOC_FLAG_GROUP)
  3721. perf_event_for_each(event, func);
  3722. else
  3723. perf_event_for_each_child(event, func);
  3724. return 0;
  3725. }
  3726. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3727. {
  3728. struct perf_event *event = file->private_data;
  3729. struct perf_event_context *ctx;
  3730. long ret;
  3731. ctx = perf_event_ctx_lock(event);
  3732. ret = _perf_ioctl(event, cmd, arg);
  3733. perf_event_ctx_unlock(event, ctx);
  3734. return ret;
  3735. }
  3736. #ifdef CONFIG_COMPAT
  3737. static long perf_compat_ioctl(struct file *file, unsigned int cmd,
  3738. unsigned long arg)
  3739. {
  3740. switch (_IOC_NR(cmd)) {
  3741. case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
  3742. case _IOC_NR(PERF_EVENT_IOC_ID):
  3743. /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
  3744. if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
  3745. cmd &= ~IOCSIZE_MASK;
  3746. cmd |= sizeof(void *) << IOCSIZE_SHIFT;
  3747. }
  3748. break;
  3749. }
  3750. return perf_ioctl(file, cmd, arg);
  3751. }
  3752. #else
  3753. # define perf_compat_ioctl NULL
  3754. #endif
  3755. int perf_event_task_enable(void)
  3756. {
  3757. struct perf_event_context *ctx;
  3758. struct perf_event *event;
  3759. mutex_lock(&current->perf_event_mutex);
  3760. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3761. ctx = perf_event_ctx_lock(event);
  3762. perf_event_for_each_child(event, _perf_event_enable);
  3763. perf_event_ctx_unlock(event, ctx);
  3764. }
  3765. mutex_unlock(&current->perf_event_mutex);
  3766. return 0;
  3767. }
  3768. int perf_event_task_disable(void)
  3769. {
  3770. struct perf_event_context *ctx;
  3771. struct perf_event *event;
  3772. mutex_lock(&current->perf_event_mutex);
  3773. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3774. ctx = perf_event_ctx_lock(event);
  3775. perf_event_for_each_child(event, _perf_event_disable);
  3776. perf_event_ctx_unlock(event, ctx);
  3777. }
  3778. mutex_unlock(&current->perf_event_mutex);
  3779. return 0;
  3780. }
  3781. static int perf_event_index(struct perf_event *event)
  3782. {
  3783. if (event->hw.state & PERF_HES_STOPPED)
  3784. return 0;
  3785. if (event->state != PERF_EVENT_STATE_ACTIVE)
  3786. return 0;
  3787. return event->pmu->event_idx(event);
  3788. }
  3789. static void calc_timer_values(struct perf_event *event,
  3790. u64 *now,
  3791. u64 *enabled,
  3792. u64 *running)
  3793. {
  3794. u64 ctx_time;
  3795. *now = perf_clock();
  3796. ctx_time = event->shadow_ctx_time + *now;
  3797. *enabled = ctx_time - event->tstamp_enabled;
  3798. *running = ctx_time - event->tstamp_running;
  3799. }
  3800. static void perf_event_init_userpage(struct perf_event *event)
  3801. {
  3802. struct perf_event_mmap_page *userpg;
  3803. struct ring_buffer *rb;
  3804. rcu_read_lock();
  3805. rb = rcu_dereference(event->rb);
  3806. if (!rb)
  3807. goto unlock;
  3808. userpg = rb->user_page;
  3809. /* Allow new userspace to detect that bit 0 is deprecated */
  3810. userpg->cap_bit0_is_deprecated = 1;
  3811. userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
  3812. userpg->data_offset = PAGE_SIZE;
  3813. userpg->data_size = perf_data_size(rb);
  3814. unlock:
  3815. rcu_read_unlock();
  3816. }
  3817. void __weak arch_perf_update_userpage(
  3818. struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
  3819. {
  3820. }
  3821. /*
  3822. * Callers need to ensure there can be no nesting of this function, otherwise
  3823. * the seqlock logic goes bad. We can not serialize this because the arch
  3824. * code calls this from NMI context.
  3825. */
  3826. void perf_event_update_userpage(struct perf_event *event)
  3827. {
  3828. struct perf_event_mmap_page *userpg;
  3829. struct ring_buffer *rb;
  3830. u64 enabled, running, now;
  3831. rcu_read_lock();
  3832. rb = rcu_dereference(event->rb);
  3833. if (!rb)
  3834. goto unlock;
  3835. /*
  3836. * compute total_time_enabled, total_time_running
  3837. * based on snapshot values taken when the event
  3838. * was last scheduled in.
  3839. *
  3840. * we cannot simply called update_context_time()
  3841. * because of locking issue as we can be called in
  3842. * NMI context
  3843. */
  3844. calc_timer_values(event, &now, &enabled, &running);
  3845. userpg = rb->user_page;
  3846. /*
  3847. * Disable preemption so as to not let the corresponding user-space
  3848. * spin too long if we get preempted.
  3849. */
  3850. preempt_disable();
  3851. ++userpg->lock;
  3852. barrier();
  3853. userpg->index = perf_event_index(event);
  3854. userpg->offset = perf_event_count(event);
  3855. if (userpg->index)
  3856. userpg->offset -= local64_read(&event->hw.prev_count);
  3857. userpg->time_enabled = enabled +
  3858. atomic64_read(&event->child_total_time_enabled);
  3859. userpg->time_running = running +
  3860. atomic64_read(&event->child_total_time_running);
  3861. arch_perf_update_userpage(event, userpg, now);
  3862. barrier();
  3863. ++userpg->lock;
  3864. preempt_enable();
  3865. unlock:
  3866. rcu_read_unlock();
  3867. }
  3868. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  3869. {
  3870. struct perf_event *event = vma->vm_file->private_data;
  3871. struct ring_buffer *rb;
  3872. int ret = VM_FAULT_SIGBUS;
  3873. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  3874. if (vmf->pgoff == 0)
  3875. ret = 0;
  3876. return ret;
  3877. }
  3878. rcu_read_lock();
  3879. rb = rcu_dereference(event->rb);
  3880. if (!rb)
  3881. goto unlock;
  3882. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  3883. goto unlock;
  3884. vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
  3885. if (!vmf->page)
  3886. goto unlock;
  3887. get_page(vmf->page);
  3888. vmf->page->mapping = vma->vm_file->f_mapping;
  3889. vmf->page->index = vmf->pgoff;
  3890. ret = 0;
  3891. unlock:
  3892. rcu_read_unlock();
  3893. return ret;
  3894. }
  3895. static void ring_buffer_attach(struct perf_event *event,
  3896. struct ring_buffer *rb)
  3897. {
  3898. struct ring_buffer *old_rb = NULL;
  3899. unsigned long flags;
  3900. if (event->rb) {
  3901. /*
  3902. * Should be impossible, we set this when removing
  3903. * event->rb_entry and wait/clear when adding event->rb_entry.
  3904. */
  3905. WARN_ON_ONCE(event->rcu_pending);
  3906. old_rb = event->rb;
  3907. spin_lock_irqsave(&old_rb->event_lock, flags);
  3908. list_del_rcu(&event->rb_entry);
  3909. spin_unlock_irqrestore(&old_rb->event_lock, flags);
  3910. event->rcu_batches = get_state_synchronize_rcu();
  3911. event->rcu_pending = 1;
  3912. }
  3913. if (rb) {
  3914. if (event->rcu_pending) {
  3915. cond_synchronize_rcu(event->rcu_batches);
  3916. event->rcu_pending = 0;
  3917. }
  3918. spin_lock_irqsave(&rb->event_lock, flags);
  3919. list_add_rcu(&event->rb_entry, &rb->event_list);
  3920. spin_unlock_irqrestore(&rb->event_lock, flags);
  3921. }
  3922. rcu_assign_pointer(event->rb, rb);
  3923. if (old_rb) {
  3924. ring_buffer_put(old_rb);
  3925. /*
  3926. * Since we detached before setting the new rb, so that we
  3927. * could attach the new rb, we could have missed a wakeup.
  3928. * Provide it now.
  3929. */
  3930. wake_up_all(&event->waitq);
  3931. }
  3932. }
  3933. static void ring_buffer_wakeup(struct perf_event *event)
  3934. {
  3935. struct ring_buffer *rb;
  3936. rcu_read_lock();
  3937. rb = rcu_dereference(event->rb);
  3938. if (rb) {
  3939. list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
  3940. wake_up_all(&event->waitq);
  3941. }
  3942. rcu_read_unlock();
  3943. }
  3944. struct ring_buffer *ring_buffer_get(struct perf_event *event)
  3945. {
  3946. struct ring_buffer *rb;
  3947. rcu_read_lock();
  3948. rb = rcu_dereference(event->rb);
  3949. if (rb) {
  3950. if (!atomic_inc_not_zero(&rb->refcount))
  3951. rb = NULL;
  3952. }
  3953. rcu_read_unlock();
  3954. return rb;
  3955. }
  3956. void ring_buffer_put(struct ring_buffer *rb)
  3957. {
  3958. if (!atomic_dec_and_test(&rb->refcount))
  3959. return;
  3960. WARN_ON_ONCE(!list_empty(&rb->event_list));
  3961. call_rcu(&rb->rcu_head, rb_free_rcu);
  3962. }
  3963. static void perf_mmap_open(struct vm_area_struct *vma)
  3964. {
  3965. struct perf_event *event = vma->vm_file->private_data;
  3966. atomic_inc(&event->mmap_count);
  3967. atomic_inc(&event->rb->mmap_count);
  3968. if (vma->vm_pgoff)
  3969. atomic_inc(&event->rb->aux_mmap_count);
  3970. if (event->pmu->event_mapped)
  3971. event->pmu->event_mapped(event);
  3972. }
  3973. static void perf_pmu_output_stop(struct perf_event *event);
  3974. /*
  3975. * A buffer can be mmap()ed multiple times; either directly through the same
  3976. * event, or through other events by use of perf_event_set_output().
  3977. *
  3978. * In order to undo the VM accounting done by perf_mmap() we need to destroy
  3979. * the buffer here, where we still have a VM context. This means we need
  3980. * to detach all events redirecting to us.
  3981. */
  3982. static void perf_mmap_close(struct vm_area_struct *vma)
  3983. {
  3984. struct perf_event *event = vma->vm_file->private_data;
  3985. struct ring_buffer *rb = ring_buffer_get(event);
  3986. struct user_struct *mmap_user = rb->mmap_user;
  3987. int mmap_locked = rb->mmap_locked;
  3988. unsigned long size = perf_data_size(rb);
  3989. if (event->pmu->event_unmapped)
  3990. event->pmu->event_unmapped(event);
  3991. /*
  3992. * rb->aux_mmap_count will always drop before rb->mmap_count and
  3993. * event->mmap_count, so it is ok to use event->mmap_mutex to
  3994. * serialize with perf_mmap here.
  3995. */
  3996. if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
  3997. atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
  3998. /*
  3999. * Stop all AUX events that are writing to this buffer,
  4000. * so that we can free its AUX pages and corresponding PMU
  4001. * data. Note that after rb::aux_mmap_count dropped to zero,
  4002. * they won't start any more (see perf_aux_output_begin()).
  4003. */
  4004. perf_pmu_output_stop(event);
  4005. /* now it's safe to free the pages */
  4006. atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
  4007. vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
  4008. /* this has to be the last one */
  4009. rb_free_aux(rb);
  4010. WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
  4011. mutex_unlock(&event->mmap_mutex);
  4012. }
  4013. atomic_dec(&rb->mmap_count);
  4014. if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
  4015. goto out_put;
  4016. ring_buffer_attach(event, NULL);
  4017. mutex_unlock(&event->mmap_mutex);
  4018. /* If there's still other mmap()s of this buffer, we're done. */
  4019. if (atomic_read(&rb->mmap_count))
  4020. goto out_put;
  4021. /*
  4022. * No other mmap()s, detach from all other events that might redirect
  4023. * into the now unreachable buffer. Somewhat complicated by the
  4024. * fact that rb::event_lock otherwise nests inside mmap_mutex.
  4025. */
  4026. again:
  4027. rcu_read_lock();
  4028. list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
  4029. if (!atomic_long_inc_not_zero(&event->refcount)) {
  4030. /*
  4031. * This event is en-route to free_event() which will
  4032. * detach it and remove it from the list.
  4033. */
  4034. continue;
  4035. }
  4036. rcu_read_unlock();
  4037. mutex_lock(&event->mmap_mutex);
  4038. /*
  4039. * Check we didn't race with perf_event_set_output() which can
  4040. * swizzle the rb from under us while we were waiting to
  4041. * acquire mmap_mutex.
  4042. *
  4043. * If we find a different rb; ignore this event, a next
  4044. * iteration will no longer find it on the list. We have to
  4045. * still restart the iteration to make sure we're not now
  4046. * iterating the wrong list.
  4047. */
  4048. if (event->rb == rb)
  4049. ring_buffer_attach(event, NULL);
  4050. mutex_unlock(&event->mmap_mutex);
  4051. put_event(event);
  4052. /*
  4053. * Restart the iteration; either we're on the wrong list or
  4054. * destroyed its integrity by doing a deletion.
  4055. */
  4056. goto again;
  4057. }
  4058. rcu_read_unlock();
  4059. /*
  4060. * It could be there's still a few 0-ref events on the list; they'll
  4061. * get cleaned up by free_event() -- they'll also still have their
  4062. * ref on the rb and will free it whenever they are done with it.
  4063. *
  4064. * Aside from that, this buffer is 'fully' detached and unmapped,
  4065. * undo the VM accounting.
  4066. */
  4067. atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
  4068. vma->vm_mm->pinned_vm -= mmap_locked;
  4069. free_uid(mmap_user);
  4070. out_put:
  4071. ring_buffer_put(rb); /* could be last */
  4072. }
  4073. static const struct vm_operations_struct perf_mmap_vmops = {
  4074. .open = perf_mmap_open,
  4075. .close = perf_mmap_close, /* non mergable */
  4076. .fault = perf_mmap_fault,
  4077. .page_mkwrite = perf_mmap_fault,
  4078. };
  4079. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  4080. {
  4081. struct perf_event *event = file->private_data;
  4082. unsigned long user_locked, user_lock_limit;
  4083. struct user_struct *user = current_user();
  4084. unsigned long locked, lock_limit;
  4085. struct ring_buffer *rb = NULL;
  4086. unsigned long vma_size;
  4087. unsigned long nr_pages;
  4088. long user_extra = 0, extra = 0;
  4089. int ret = 0, flags = 0;
  4090. /*
  4091. * Don't allow mmap() of inherited per-task counters. This would
  4092. * create a performance issue due to all children writing to the
  4093. * same rb.
  4094. */
  4095. if (event->cpu == -1 && event->attr.inherit)
  4096. return -EINVAL;
  4097. if (!(vma->vm_flags & VM_SHARED))
  4098. return -EINVAL;
  4099. vma_size = vma->vm_end - vma->vm_start;
  4100. if (vma->vm_pgoff == 0) {
  4101. nr_pages = (vma_size / PAGE_SIZE) - 1;
  4102. } else {
  4103. /*
  4104. * AUX area mapping: if rb->aux_nr_pages != 0, it's already
  4105. * mapped, all subsequent mappings should have the same size
  4106. * and offset. Must be above the normal perf buffer.
  4107. */
  4108. u64 aux_offset, aux_size;
  4109. if (!event->rb)
  4110. return -EINVAL;
  4111. nr_pages = vma_size / PAGE_SIZE;
  4112. mutex_lock(&event->mmap_mutex);
  4113. ret = -EINVAL;
  4114. rb = event->rb;
  4115. if (!rb)
  4116. goto aux_unlock;
  4117. aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
  4118. aux_size = ACCESS_ONCE(rb->user_page->aux_size);
  4119. if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
  4120. goto aux_unlock;
  4121. if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
  4122. goto aux_unlock;
  4123. /* already mapped with a different offset */
  4124. if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
  4125. goto aux_unlock;
  4126. if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
  4127. goto aux_unlock;
  4128. /* already mapped with a different size */
  4129. if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
  4130. goto aux_unlock;
  4131. if (!is_power_of_2(nr_pages))
  4132. goto aux_unlock;
  4133. if (!atomic_inc_not_zero(&rb->mmap_count))
  4134. goto aux_unlock;
  4135. if (rb_has_aux(rb)) {
  4136. atomic_inc(&rb->aux_mmap_count);
  4137. ret = 0;
  4138. goto unlock;
  4139. }
  4140. atomic_set(&rb->aux_mmap_count, 1);
  4141. user_extra = nr_pages;
  4142. goto accounting;
  4143. }
  4144. /*
  4145. * If we have rb pages ensure they're a power-of-two number, so we
  4146. * can do bitmasks instead of modulo.
  4147. */
  4148. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  4149. return -EINVAL;
  4150. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  4151. return -EINVAL;
  4152. WARN_ON_ONCE(event->ctx->parent_ctx);
  4153. again:
  4154. mutex_lock(&event->mmap_mutex);
  4155. if (event->rb) {
  4156. if (event->rb->nr_pages != nr_pages) {
  4157. ret = -EINVAL;
  4158. goto unlock;
  4159. }
  4160. if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
  4161. /*
  4162. * Raced against perf_mmap_close() through
  4163. * perf_event_set_output(). Try again, hope for better
  4164. * luck.
  4165. */
  4166. mutex_unlock(&event->mmap_mutex);
  4167. goto again;
  4168. }
  4169. goto unlock;
  4170. }
  4171. user_extra = nr_pages + 1;
  4172. accounting:
  4173. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  4174. /*
  4175. * Increase the limit linearly with more CPUs:
  4176. */
  4177. user_lock_limit *= num_online_cpus();
  4178. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  4179. if (user_locked > user_lock_limit)
  4180. extra = user_locked - user_lock_limit;
  4181. lock_limit = rlimit(RLIMIT_MEMLOCK);
  4182. lock_limit >>= PAGE_SHIFT;
  4183. locked = vma->vm_mm->pinned_vm + extra;
  4184. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  4185. !capable(CAP_IPC_LOCK)) {
  4186. ret = -EPERM;
  4187. goto unlock;
  4188. }
  4189. WARN_ON(!rb && event->rb);
  4190. if (vma->vm_flags & VM_WRITE)
  4191. flags |= RING_BUFFER_WRITABLE;
  4192. if (!rb) {
  4193. rb = rb_alloc(nr_pages,
  4194. event->attr.watermark ? event->attr.wakeup_watermark : 0,
  4195. event->cpu, flags);
  4196. if (!rb) {
  4197. ret = -ENOMEM;
  4198. goto unlock;
  4199. }
  4200. atomic_set(&rb->mmap_count, 1);
  4201. rb->mmap_user = get_current_user();
  4202. rb->mmap_locked = extra;
  4203. ring_buffer_attach(event, rb);
  4204. perf_event_init_userpage(event);
  4205. perf_event_update_userpage(event);
  4206. } else {
  4207. ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
  4208. event->attr.aux_watermark, flags);
  4209. if (!ret)
  4210. rb->aux_mmap_locked = extra;
  4211. }
  4212. unlock:
  4213. if (!ret) {
  4214. atomic_long_add(user_extra, &user->locked_vm);
  4215. vma->vm_mm->pinned_vm += extra;
  4216. atomic_inc(&event->mmap_count);
  4217. } else if (rb) {
  4218. atomic_dec(&rb->mmap_count);
  4219. }
  4220. aux_unlock:
  4221. mutex_unlock(&event->mmap_mutex);
  4222. /*
  4223. * Since pinned accounting is per vm we cannot allow fork() to copy our
  4224. * vma.
  4225. */
  4226. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
  4227. vma->vm_ops = &perf_mmap_vmops;
  4228. if (event->pmu->event_mapped)
  4229. event->pmu->event_mapped(event);
  4230. return ret;
  4231. }
  4232. static int perf_fasync(int fd, struct file *filp, int on)
  4233. {
  4234. struct inode *inode = file_inode(filp);
  4235. struct perf_event *event = filp->private_data;
  4236. int retval;
  4237. inode_lock(inode);
  4238. retval = fasync_helper(fd, filp, on, &event->fasync);
  4239. inode_unlock(inode);
  4240. if (retval < 0)
  4241. return retval;
  4242. return 0;
  4243. }
  4244. static const struct file_operations perf_fops = {
  4245. .llseek = no_llseek,
  4246. .release = perf_release,
  4247. .read = perf_read,
  4248. .poll = perf_poll,
  4249. .unlocked_ioctl = perf_ioctl,
  4250. .compat_ioctl = perf_compat_ioctl,
  4251. .mmap = perf_mmap,
  4252. .fasync = perf_fasync,
  4253. };
  4254. /*
  4255. * Perf event wakeup
  4256. *
  4257. * If there's data, ensure we set the poll() state and publish everything
  4258. * to user-space before waking everybody up.
  4259. */
  4260. static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
  4261. {
  4262. /* only the parent has fasync state */
  4263. if (event->parent)
  4264. event = event->parent;
  4265. return &event->fasync;
  4266. }
  4267. void perf_event_wakeup(struct perf_event *event)
  4268. {
  4269. ring_buffer_wakeup(event);
  4270. if (event->pending_kill) {
  4271. kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
  4272. event->pending_kill = 0;
  4273. }
  4274. }
  4275. static void perf_pending_event(struct irq_work *entry)
  4276. {
  4277. struct perf_event *event = container_of(entry,
  4278. struct perf_event, pending);
  4279. int rctx;
  4280. rctx = perf_swevent_get_recursion_context();
  4281. /*
  4282. * If we 'fail' here, that's OK, it means recursion is already disabled
  4283. * and we won't recurse 'further'.
  4284. */
  4285. if (event->pending_disable) {
  4286. event->pending_disable = 0;
  4287. perf_event_disable_local(event);
  4288. }
  4289. if (event->pending_wakeup) {
  4290. event->pending_wakeup = 0;
  4291. perf_event_wakeup(event);
  4292. }
  4293. if (rctx >= 0)
  4294. perf_swevent_put_recursion_context(rctx);
  4295. }
  4296. /*
  4297. * We assume there is only KVM supporting the callbacks.
  4298. * Later on, we might change it to a list if there is
  4299. * another virtualization implementation supporting the callbacks.
  4300. */
  4301. struct perf_guest_info_callbacks *perf_guest_cbs;
  4302. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  4303. {
  4304. perf_guest_cbs = cbs;
  4305. return 0;
  4306. }
  4307. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  4308. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  4309. {
  4310. perf_guest_cbs = NULL;
  4311. return 0;
  4312. }
  4313. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  4314. static void
  4315. perf_output_sample_regs(struct perf_output_handle *handle,
  4316. struct pt_regs *regs, u64 mask)
  4317. {
  4318. int bit;
  4319. for_each_set_bit(bit, (const unsigned long *) &mask,
  4320. sizeof(mask) * BITS_PER_BYTE) {
  4321. u64 val;
  4322. val = perf_reg_value(regs, bit);
  4323. perf_output_put(handle, val);
  4324. }
  4325. }
  4326. static void perf_sample_regs_user(struct perf_regs *regs_user,
  4327. struct pt_regs *regs,
  4328. struct pt_regs *regs_user_copy)
  4329. {
  4330. if (user_mode(regs)) {
  4331. regs_user->abi = perf_reg_abi(current);
  4332. regs_user->regs = regs;
  4333. } else if (current->mm) {
  4334. perf_get_regs_user(regs_user, regs, regs_user_copy);
  4335. } else {
  4336. regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
  4337. regs_user->regs = NULL;
  4338. }
  4339. }
  4340. static void perf_sample_regs_intr(struct perf_regs *regs_intr,
  4341. struct pt_regs *regs)
  4342. {
  4343. regs_intr->regs = regs;
  4344. regs_intr->abi = perf_reg_abi(current);
  4345. }
  4346. /*
  4347. * Get remaining task size from user stack pointer.
  4348. *
  4349. * It'd be better to take stack vma map and limit this more
  4350. * precisly, but there's no way to get it safely under interrupt,
  4351. * so using TASK_SIZE as limit.
  4352. */
  4353. static u64 perf_ustack_task_size(struct pt_regs *regs)
  4354. {
  4355. unsigned long addr = perf_user_stack_pointer(regs);
  4356. if (!addr || addr >= TASK_SIZE)
  4357. return 0;
  4358. return TASK_SIZE - addr;
  4359. }
  4360. static u16
  4361. perf_sample_ustack_size(u16 stack_size, u16 header_size,
  4362. struct pt_regs *regs)
  4363. {
  4364. u64 task_size;
  4365. /* No regs, no stack pointer, no dump. */
  4366. if (!regs)
  4367. return 0;
  4368. /*
  4369. * Check if we fit in with the requested stack size into the:
  4370. * - TASK_SIZE
  4371. * If we don't, we limit the size to the TASK_SIZE.
  4372. *
  4373. * - remaining sample size
  4374. * If we don't, we customize the stack size to
  4375. * fit in to the remaining sample size.
  4376. */
  4377. task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
  4378. stack_size = min(stack_size, (u16) task_size);
  4379. /* Current header size plus static size and dynamic size. */
  4380. header_size += 2 * sizeof(u64);
  4381. /* Do we fit in with the current stack dump size? */
  4382. if ((u16) (header_size + stack_size) < header_size) {
  4383. /*
  4384. * If we overflow the maximum size for the sample,
  4385. * we customize the stack dump size to fit in.
  4386. */
  4387. stack_size = USHRT_MAX - header_size - sizeof(u64);
  4388. stack_size = round_up(stack_size, sizeof(u64));
  4389. }
  4390. return stack_size;
  4391. }
  4392. static void
  4393. perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
  4394. struct pt_regs *regs)
  4395. {
  4396. /* Case of a kernel thread, nothing to dump */
  4397. if (!regs) {
  4398. u64 size = 0;
  4399. perf_output_put(handle, size);
  4400. } else {
  4401. unsigned long sp;
  4402. unsigned int rem;
  4403. u64 dyn_size;
  4404. /*
  4405. * We dump:
  4406. * static size
  4407. * - the size requested by user or the best one we can fit
  4408. * in to the sample max size
  4409. * data
  4410. * - user stack dump data
  4411. * dynamic size
  4412. * - the actual dumped size
  4413. */
  4414. /* Static size. */
  4415. perf_output_put(handle, dump_size);
  4416. /* Data. */
  4417. sp = perf_user_stack_pointer(regs);
  4418. rem = __output_copy_user(handle, (void *) sp, dump_size);
  4419. dyn_size = dump_size - rem;
  4420. perf_output_skip(handle, rem);
  4421. /* Dynamic size. */
  4422. perf_output_put(handle, dyn_size);
  4423. }
  4424. }
  4425. static void __perf_event_header__init_id(struct perf_event_header *header,
  4426. struct perf_sample_data *data,
  4427. struct perf_event *event)
  4428. {
  4429. u64 sample_type = event->attr.sample_type;
  4430. data->type = sample_type;
  4431. header->size += event->id_header_size;
  4432. if (sample_type & PERF_SAMPLE_TID) {
  4433. /* namespace issues */
  4434. data->tid_entry.pid = perf_event_pid(event, current);
  4435. data->tid_entry.tid = perf_event_tid(event, current);
  4436. }
  4437. if (sample_type & PERF_SAMPLE_TIME)
  4438. data->time = perf_event_clock(event);
  4439. if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
  4440. data->id = primary_event_id(event);
  4441. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4442. data->stream_id = event->id;
  4443. if (sample_type & PERF_SAMPLE_CPU) {
  4444. data->cpu_entry.cpu = raw_smp_processor_id();
  4445. data->cpu_entry.reserved = 0;
  4446. }
  4447. }
  4448. void perf_event_header__init_id(struct perf_event_header *header,
  4449. struct perf_sample_data *data,
  4450. struct perf_event *event)
  4451. {
  4452. if (event->attr.sample_id_all)
  4453. __perf_event_header__init_id(header, data, event);
  4454. }
  4455. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  4456. struct perf_sample_data *data)
  4457. {
  4458. u64 sample_type = data->type;
  4459. if (sample_type & PERF_SAMPLE_TID)
  4460. perf_output_put(handle, data->tid_entry);
  4461. if (sample_type & PERF_SAMPLE_TIME)
  4462. perf_output_put(handle, data->time);
  4463. if (sample_type & PERF_SAMPLE_ID)
  4464. perf_output_put(handle, data->id);
  4465. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4466. perf_output_put(handle, data->stream_id);
  4467. if (sample_type & PERF_SAMPLE_CPU)
  4468. perf_output_put(handle, data->cpu_entry);
  4469. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  4470. perf_output_put(handle, data->id);
  4471. }
  4472. void perf_event__output_id_sample(struct perf_event *event,
  4473. struct perf_output_handle *handle,
  4474. struct perf_sample_data *sample)
  4475. {
  4476. if (event->attr.sample_id_all)
  4477. __perf_event__output_id_sample(handle, sample);
  4478. }
  4479. static void perf_output_read_one(struct perf_output_handle *handle,
  4480. struct perf_event *event,
  4481. u64 enabled, u64 running)
  4482. {
  4483. u64 read_format = event->attr.read_format;
  4484. u64 values[4];
  4485. int n = 0;
  4486. values[n++] = perf_event_count(event);
  4487. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  4488. values[n++] = enabled +
  4489. atomic64_read(&event->child_total_time_enabled);
  4490. }
  4491. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  4492. values[n++] = running +
  4493. atomic64_read(&event->child_total_time_running);
  4494. }
  4495. if (read_format & PERF_FORMAT_ID)
  4496. values[n++] = primary_event_id(event);
  4497. __output_copy(handle, values, n * sizeof(u64));
  4498. }
  4499. /*
  4500. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  4501. */
  4502. static void perf_output_read_group(struct perf_output_handle *handle,
  4503. struct perf_event *event,
  4504. u64 enabled, u64 running)
  4505. {
  4506. struct perf_event *leader = event->group_leader, *sub;
  4507. u64 read_format = event->attr.read_format;
  4508. u64 values[5];
  4509. int n = 0;
  4510. values[n++] = 1 + leader->nr_siblings;
  4511. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  4512. values[n++] = enabled;
  4513. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  4514. values[n++] = running;
  4515. if (leader != event)
  4516. leader->pmu->read(leader);
  4517. values[n++] = perf_event_count(leader);
  4518. if (read_format & PERF_FORMAT_ID)
  4519. values[n++] = primary_event_id(leader);
  4520. __output_copy(handle, values, n * sizeof(u64));
  4521. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  4522. n = 0;
  4523. if ((sub != event) &&
  4524. (sub->state == PERF_EVENT_STATE_ACTIVE))
  4525. sub->pmu->read(sub);
  4526. values[n++] = perf_event_count(sub);
  4527. if (read_format & PERF_FORMAT_ID)
  4528. values[n++] = primary_event_id(sub);
  4529. __output_copy(handle, values, n * sizeof(u64));
  4530. }
  4531. }
  4532. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  4533. PERF_FORMAT_TOTAL_TIME_RUNNING)
  4534. static void perf_output_read(struct perf_output_handle *handle,
  4535. struct perf_event *event)
  4536. {
  4537. u64 enabled = 0, running = 0, now;
  4538. u64 read_format = event->attr.read_format;
  4539. /*
  4540. * compute total_time_enabled, total_time_running
  4541. * based on snapshot values taken when the event
  4542. * was last scheduled in.
  4543. *
  4544. * we cannot simply called update_context_time()
  4545. * because of locking issue as we are called in
  4546. * NMI context
  4547. */
  4548. if (read_format & PERF_FORMAT_TOTAL_TIMES)
  4549. calc_timer_values(event, &now, &enabled, &running);
  4550. if (event->attr.read_format & PERF_FORMAT_GROUP)
  4551. perf_output_read_group(handle, event, enabled, running);
  4552. else
  4553. perf_output_read_one(handle, event, enabled, running);
  4554. }
  4555. void perf_output_sample(struct perf_output_handle *handle,
  4556. struct perf_event_header *header,
  4557. struct perf_sample_data *data,
  4558. struct perf_event *event)
  4559. {
  4560. u64 sample_type = data->type;
  4561. perf_output_put(handle, *header);
  4562. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  4563. perf_output_put(handle, data->id);
  4564. if (sample_type & PERF_SAMPLE_IP)
  4565. perf_output_put(handle, data->ip);
  4566. if (sample_type & PERF_SAMPLE_TID)
  4567. perf_output_put(handle, data->tid_entry);
  4568. if (sample_type & PERF_SAMPLE_TIME)
  4569. perf_output_put(handle, data->time);
  4570. if (sample_type & PERF_SAMPLE_ADDR)
  4571. perf_output_put(handle, data->addr);
  4572. if (sample_type & PERF_SAMPLE_ID)
  4573. perf_output_put(handle, data->id);
  4574. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4575. perf_output_put(handle, data->stream_id);
  4576. if (sample_type & PERF_SAMPLE_CPU)
  4577. perf_output_put(handle, data->cpu_entry);
  4578. if (sample_type & PERF_SAMPLE_PERIOD)
  4579. perf_output_put(handle, data->period);
  4580. if (sample_type & PERF_SAMPLE_READ)
  4581. perf_output_read(handle, event);
  4582. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  4583. if (data->callchain) {
  4584. int size = 1;
  4585. if (data->callchain)
  4586. size += data->callchain->nr;
  4587. size *= sizeof(u64);
  4588. __output_copy(handle, data->callchain, size);
  4589. } else {
  4590. u64 nr = 0;
  4591. perf_output_put(handle, nr);
  4592. }
  4593. }
  4594. if (sample_type & PERF_SAMPLE_RAW) {
  4595. if (data->raw) {
  4596. u32 raw_size = data->raw->size;
  4597. u32 real_size = round_up(raw_size + sizeof(u32),
  4598. sizeof(u64)) - sizeof(u32);
  4599. u64 zero = 0;
  4600. perf_output_put(handle, real_size);
  4601. __output_copy(handle, data->raw->data, raw_size);
  4602. if (real_size - raw_size)
  4603. __output_copy(handle, &zero, real_size - raw_size);
  4604. } else {
  4605. struct {
  4606. u32 size;
  4607. u32 data;
  4608. } raw = {
  4609. .size = sizeof(u32),
  4610. .data = 0,
  4611. };
  4612. perf_output_put(handle, raw);
  4613. }
  4614. }
  4615. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  4616. if (data->br_stack) {
  4617. size_t size;
  4618. size = data->br_stack->nr
  4619. * sizeof(struct perf_branch_entry);
  4620. perf_output_put(handle, data->br_stack->nr);
  4621. perf_output_copy(handle, data->br_stack->entries, size);
  4622. } else {
  4623. /*
  4624. * we always store at least the value of nr
  4625. */
  4626. u64 nr = 0;
  4627. perf_output_put(handle, nr);
  4628. }
  4629. }
  4630. if (sample_type & PERF_SAMPLE_REGS_USER) {
  4631. u64 abi = data->regs_user.abi;
  4632. /*
  4633. * If there are no regs to dump, notice it through
  4634. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  4635. */
  4636. perf_output_put(handle, abi);
  4637. if (abi) {
  4638. u64 mask = event->attr.sample_regs_user;
  4639. perf_output_sample_regs(handle,
  4640. data->regs_user.regs,
  4641. mask);
  4642. }
  4643. }
  4644. if (sample_type & PERF_SAMPLE_STACK_USER) {
  4645. perf_output_sample_ustack(handle,
  4646. data->stack_user_size,
  4647. data->regs_user.regs);
  4648. }
  4649. if (sample_type & PERF_SAMPLE_WEIGHT)
  4650. perf_output_put(handle, data->weight);
  4651. if (sample_type & PERF_SAMPLE_DATA_SRC)
  4652. perf_output_put(handle, data->data_src.val);
  4653. if (sample_type & PERF_SAMPLE_TRANSACTION)
  4654. perf_output_put(handle, data->txn);
  4655. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  4656. u64 abi = data->regs_intr.abi;
  4657. /*
  4658. * If there are no regs to dump, notice it through
  4659. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  4660. */
  4661. perf_output_put(handle, abi);
  4662. if (abi) {
  4663. u64 mask = event->attr.sample_regs_intr;
  4664. perf_output_sample_regs(handle,
  4665. data->regs_intr.regs,
  4666. mask);
  4667. }
  4668. }
  4669. if (!event->attr.watermark) {
  4670. int wakeup_events = event->attr.wakeup_events;
  4671. if (wakeup_events) {
  4672. struct ring_buffer *rb = handle->rb;
  4673. int events = local_inc_return(&rb->events);
  4674. if (events >= wakeup_events) {
  4675. local_sub(wakeup_events, &rb->events);
  4676. local_inc(&rb->wakeup);
  4677. }
  4678. }
  4679. }
  4680. }
  4681. void perf_prepare_sample(struct perf_event_header *header,
  4682. struct perf_sample_data *data,
  4683. struct perf_event *event,
  4684. struct pt_regs *regs)
  4685. {
  4686. u64 sample_type = event->attr.sample_type;
  4687. header->type = PERF_RECORD_SAMPLE;
  4688. header->size = sizeof(*header) + event->header_size;
  4689. header->misc = 0;
  4690. header->misc |= perf_misc_flags(regs);
  4691. __perf_event_header__init_id(header, data, event);
  4692. if (sample_type & PERF_SAMPLE_IP)
  4693. data->ip = perf_instruction_pointer(regs);
  4694. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  4695. int size = 1;
  4696. data->callchain = perf_callchain(event, regs);
  4697. if (data->callchain)
  4698. size += data->callchain->nr;
  4699. header->size += size * sizeof(u64);
  4700. }
  4701. if (sample_type & PERF_SAMPLE_RAW) {
  4702. int size = sizeof(u32);
  4703. if (data->raw)
  4704. size += data->raw->size;
  4705. else
  4706. size += sizeof(u32);
  4707. header->size += round_up(size, sizeof(u64));
  4708. }
  4709. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  4710. int size = sizeof(u64); /* nr */
  4711. if (data->br_stack) {
  4712. size += data->br_stack->nr
  4713. * sizeof(struct perf_branch_entry);
  4714. }
  4715. header->size += size;
  4716. }
  4717. if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
  4718. perf_sample_regs_user(&data->regs_user, regs,
  4719. &data->regs_user_copy);
  4720. if (sample_type & PERF_SAMPLE_REGS_USER) {
  4721. /* regs dump ABI info */
  4722. int size = sizeof(u64);
  4723. if (data->regs_user.regs) {
  4724. u64 mask = event->attr.sample_regs_user;
  4725. size += hweight64(mask) * sizeof(u64);
  4726. }
  4727. header->size += size;
  4728. }
  4729. if (sample_type & PERF_SAMPLE_STACK_USER) {
  4730. /*
  4731. * Either we need PERF_SAMPLE_STACK_USER bit to be allways
  4732. * processed as the last one or have additional check added
  4733. * in case new sample type is added, because we could eat
  4734. * up the rest of the sample size.
  4735. */
  4736. u16 stack_size = event->attr.sample_stack_user;
  4737. u16 size = sizeof(u64);
  4738. stack_size = perf_sample_ustack_size(stack_size, header->size,
  4739. data->regs_user.regs);
  4740. /*
  4741. * If there is something to dump, add space for the dump
  4742. * itself and for the field that tells the dynamic size,
  4743. * which is how many have been actually dumped.
  4744. */
  4745. if (stack_size)
  4746. size += sizeof(u64) + stack_size;
  4747. data->stack_user_size = stack_size;
  4748. header->size += size;
  4749. }
  4750. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  4751. /* regs dump ABI info */
  4752. int size = sizeof(u64);
  4753. perf_sample_regs_intr(&data->regs_intr, regs);
  4754. if (data->regs_intr.regs) {
  4755. u64 mask = event->attr.sample_regs_intr;
  4756. size += hweight64(mask) * sizeof(u64);
  4757. }
  4758. header->size += size;
  4759. }
  4760. }
  4761. static void __always_inline
  4762. __perf_event_output(struct perf_event *event,
  4763. struct perf_sample_data *data,
  4764. struct pt_regs *regs,
  4765. int (*output_begin)(struct perf_output_handle *,
  4766. struct perf_event *,
  4767. unsigned int))
  4768. {
  4769. struct perf_output_handle handle;
  4770. struct perf_event_header header;
  4771. /* protect the callchain buffers */
  4772. rcu_read_lock();
  4773. perf_prepare_sample(&header, data, event, regs);
  4774. if (output_begin(&handle, event, header.size))
  4775. goto exit;
  4776. perf_output_sample(&handle, &header, data, event);
  4777. perf_output_end(&handle);
  4778. exit:
  4779. rcu_read_unlock();
  4780. }
  4781. void
  4782. perf_event_output_forward(struct perf_event *event,
  4783. struct perf_sample_data *data,
  4784. struct pt_regs *regs)
  4785. {
  4786. __perf_event_output(event, data, regs, perf_output_begin_forward);
  4787. }
  4788. void
  4789. perf_event_output_backward(struct perf_event *event,
  4790. struct perf_sample_data *data,
  4791. struct pt_regs *regs)
  4792. {
  4793. __perf_event_output(event, data, regs, perf_output_begin_backward);
  4794. }
  4795. void
  4796. perf_event_output(struct perf_event *event,
  4797. struct perf_sample_data *data,
  4798. struct pt_regs *regs)
  4799. {
  4800. __perf_event_output(event, data, regs, perf_output_begin);
  4801. }
  4802. /*
  4803. * read event_id
  4804. */
  4805. struct perf_read_event {
  4806. struct perf_event_header header;
  4807. u32 pid;
  4808. u32 tid;
  4809. };
  4810. static void
  4811. perf_event_read_event(struct perf_event *event,
  4812. struct task_struct *task)
  4813. {
  4814. struct perf_output_handle handle;
  4815. struct perf_sample_data sample;
  4816. struct perf_read_event read_event = {
  4817. .header = {
  4818. .type = PERF_RECORD_READ,
  4819. .misc = 0,
  4820. .size = sizeof(read_event) + event->read_size,
  4821. },
  4822. .pid = perf_event_pid(event, task),
  4823. .tid = perf_event_tid(event, task),
  4824. };
  4825. int ret;
  4826. perf_event_header__init_id(&read_event.header, &sample, event);
  4827. ret = perf_output_begin(&handle, event, read_event.header.size);
  4828. if (ret)
  4829. return;
  4830. perf_output_put(&handle, read_event);
  4831. perf_output_read(&handle, event);
  4832. perf_event__output_id_sample(event, &handle, &sample);
  4833. perf_output_end(&handle);
  4834. }
  4835. typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
  4836. static void
  4837. perf_event_aux_ctx(struct perf_event_context *ctx,
  4838. perf_event_aux_output_cb output,
  4839. void *data, bool all)
  4840. {
  4841. struct perf_event *event;
  4842. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  4843. if (!all) {
  4844. if (event->state < PERF_EVENT_STATE_INACTIVE)
  4845. continue;
  4846. if (!event_filter_match(event))
  4847. continue;
  4848. }
  4849. output(event, data);
  4850. }
  4851. }
  4852. static void
  4853. perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
  4854. struct perf_event_context *task_ctx)
  4855. {
  4856. rcu_read_lock();
  4857. preempt_disable();
  4858. perf_event_aux_ctx(task_ctx, output, data, false);
  4859. preempt_enable();
  4860. rcu_read_unlock();
  4861. }
  4862. static void
  4863. perf_event_aux(perf_event_aux_output_cb output, void *data,
  4864. struct perf_event_context *task_ctx)
  4865. {
  4866. struct perf_cpu_context *cpuctx;
  4867. struct perf_event_context *ctx;
  4868. struct pmu *pmu;
  4869. int ctxn;
  4870. /*
  4871. * If we have task_ctx != NULL we only notify
  4872. * the task context itself. The task_ctx is set
  4873. * only for EXIT events before releasing task
  4874. * context.
  4875. */
  4876. if (task_ctx) {
  4877. perf_event_aux_task_ctx(output, data, task_ctx);
  4878. return;
  4879. }
  4880. rcu_read_lock();
  4881. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4882. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  4883. if (cpuctx->unique_pmu != pmu)
  4884. goto next;
  4885. perf_event_aux_ctx(&cpuctx->ctx, output, data, false);
  4886. ctxn = pmu->task_ctx_nr;
  4887. if (ctxn < 0)
  4888. goto next;
  4889. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  4890. if (ctx)
  4891. perf_event_aux_ctx(ctx, output, data, false);
  4892. next:
  4893. put_cpu_ptr(pmu->pmu_cpu_context);
  4894. }
  4895. rcu_read_unlock();
  4896. }
  4897. /*
  4898. * Clear all file-based filters at exec, they'll have to be
  4899. * re-instated when/if these objects are mmapped again.
  4900. */
  4901. static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
  4902. {
  4903. struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
  4904. struct perf_addr_filter *filter;
  4905. unsigned int restart = 0, count = 0;
  4906. unsigned long flags;
  4907. if (!has_addr_filter(event))
  4908. return;
  4909. raw_spin_lock_irqsave(&ifh->lock, flags);
  4910. list_for_each_entry(filter, &ifh->list, entry) {
  4911. if (filter->inode) {
  4912. event->addr_filters_offs[count] = 0;
  4913. restart++;
  4914. }
  4915. count++;
  4916. }
  4917. if (restart)
  4918. event->addr_filters_gen++;
  4919. raw_spin_unlock_irqrestore(&ifh->lock, flags);
  4920. if (restart)
  4921. perf_event_restart(event);
  4922. }
  4923. void perf_event_exec(void)
  4924. {
  4925. struct perf_event_context *ctx;
  4926. int ctxn;
  4927. rcu_read_lock();
  4928. for_each_task_context_nr(ctxn) {
  4929. ctx = current->perf_event_ctxp[ctxn];
  4930. if (!ctx)
  4931. continue;
  4932. perf_event_enable_on_exec(ctxn);
  4933. perf_event_aux_ctx(ctx, perf_event_addr_filters_exec, NULL,
  4934. true);
  4935. }
  4936. rcu_read_unlock();
  4937. }
  4938. struct remote_output {
  4939. struct ring_buffer *rb;
  4940. int err;
  4941. };
  4942. static void __perf_event_output_stop(struct perf_event *event, void *data)
  4943. {
  4944. struct perf_event *parent = event->parent;
  4945. struct remote_output *ro = data;
  4946. struct ring_buffer *rb = ro->rb;
  4947. struct stop_event_data sd = {
  4948. .event = event,
  4949. };
  4950. if (!has_aux(event))
  4951. return;
  4952. if (!parent)
  4953. parent = event;
  4954. /*
  4955. * In case of inheritance, it will be the parent that links to the
  4956. * ring-buffer, but it will be the child that's actually using it:
  4957. */
  4958. if (rcu_dereference(parent->rb) == rb)
  4959. ro->err = __perf_event_stop(&sd);
  4960. }
  4961. static int __perf_pmu_output_stop(void *info)
  4962. {
  4963. struct perf_event *event = info;
  4964. struct pmu *pmu = event->pmu;
  4965. struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  4966. struct remote_output ro = {
  4967. .rb = event->rb,
  4968. };
  4969. rcu_read_lock();
  4970. perf_event_aux_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
  4971. if (cpuctx->task_ctx)
  4972. perf_event_aux_ctx(cpuctx->task_ctx, __perf_event_output_stop,
  4973. &ro, false);
  4974. rcu_read_unlock();
  4975. return ro.err;
  4976. }
  4977. static void perf_pmu_output_stop(struct perf_event *event)
  4978. {
  4979. struct perf_event *iter;
  4980. int err, cpu;
  4981. restart:
  4982. rcu_read_lock();
  4983. list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
  4984. /*
  4985. * For per-CPU events, we need to make sure that neither they
  4986. * nor their children are running; for cpu==-1 events it's
  4987. * sufficient to stop the event itself if it's active, since
  4988. * it can't have children.
  4989. */
  4990. cpu = iter->cpu;
  4991. if (cpu == -1)
  4992. cpu = READ_ONCE(iter->oncpu);
  4993. if (cpu == -1)
  4994. continue;
  4995. err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
  4996. if (err == -EAGAIN) {
  4997. rcu_read_unlock();
  4998. goto restart;
  4999. }
  5000. }
  5001. rcu_read_unlock();
  5002. }
  5003. /*
  5004. * task tracking -- fork/exit
  5005. *
  5006. * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
  5007. */
  5008. struct perf_task_event {
  5009. struct task_struct *task;
  5010. struct perf_event_context *task_ctx;
  5011. struct {
  5012. struct perf_event_header header;
  5013. u32 pid;
  5014. u32 ppid;
  5015. u32 tid;
  5016. u32 ptid;
  5017. u64 time;
  5018. } event_id;
  5019. };
  5020. static int perf_event_task_match(struct perf_event *event)
  5021. {
  5022. return event->attr.comm || event->attr.mmap ||
  5023. event->attr.mmap2 || event->attr.mmap_data ||
  5024. event->attr.task;
  5025. }
  5026. static void perf_event_task_output(struct perf_event *event,
  5027. void *data)
  5028. {
  5029. struct perf_task_event *task_event = data;
  5030. struct perf_output_handle handle;
  5031. struct perf_sample_data sample;
  5032. struct task_struct *task = task_event->task;
  5033. int ret, size = task_event->event_id.header.size;
  5034. if (!perf_event_task_match(event))
  5035. return;
  5036. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  5037. ret = perf_output_begin(&handle, event,
  5038. task_event->event_id.header.size);
  5039. if (ret)
  5040. goto out;
  5041. task_event->event_id.pid = perf_event_pid(event, task);
  5042. task_event->event_id.ppid = perf_event_pid(event, current);
  5043. task_event->event_id.tid = perf_event_tid(event, task);
  5044. task_event->event_id.ptid = perf_event_tid(event, current);
  5045. task_event->event_id.time = perf_event_clock(event);
  5046. perf_output_put(&handle, task_event->event_id);
  5047. perf_event__output_id_sample(event, &handle, &sample);
  5048. perf_output_end(&handle);
  5049. out:
  5050. task_event->event_id.header.size = size;
  5051. }
  5052. static void perf_event_task(struct task_struct *task,
  5053. struct perf_event_context *task_ctx,
  5054. int new)
  5055. {
  5056. struct perf_task_event task_event;
  5057. if (!atomic_read(&nr_comm_events) &&
  5058. !atomic_read(&nr_mmap_events) &&
  5059. !atomic_read(&nr_task_events))
  5060. return;
  5061. task_event = (struct perf_task_event){
  5062. .task = task,
  5063. .task_ctx = task_ctx,
  5064. .event_id = {
  5065. .header = {
  5066. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  5067. .misc = 0,
  5068. .size = sizeof(task_event.event_id),
  5069. },
  5070. /* .pid */
  5071. /* .ppid */
  5072. /* .tid */
  5073. /* .ptid */
  5074. /* .time */
  5075. },
  5076. };
  5077. perf_event_aux(perf_event_task_output,
  5078. &task_event,
  5079. task_ctx);
  5080. }
  5081. void perf_event_fork(struct task_struct *task)
  5082. {
  5083. perf_event_task(task, NULL, 1);
  5084. }
  5085. /*
  5086. * comm tracking
  5087. */
  5088. struct perf_comm_event {
  5089. struct task_struct *task;
  5090. char *comm;
  5091. int comm_size;
  5092. struct {
  5093. struct perf_event_header header;
  5094. u32 pid;
  5095. u32 tid;
  5096. } event_id;
  5097. };
  5098. static int perf_event_comm_match(struct perf_event *event)
  5099. {
  5100. return event->attr.comm;
  5101. }
  5102. static void perf_event_comm_output(struct perf_event *event,
  5103. void *data)
  5104. {
  5105. struct perf_comm_event *comm_event = data;
  5106. struct perf_output_handle handle;
  5107. struct perf_sample_data sample;
  5108. int size = comm_event->event_id.header.size;
  5109. int ret;
  5110. if (!perf_event_comm_match(event))
  5111. return;
  5112. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  5113. ret = perf_output_begin(&handle, event,
  5114. comm_event->event_id.header.size);
  5115. if (ret)
  5116. goto out;
  5117. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  5118. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  5119. perf_output_put(&handle, comm_event->event_id);
  5120. __output_copy(&handle, comm_event->comm,
  5121. comm_event->comm_size);
  5122. perf_event__output_id_sample(event, &handle, &sample);
  5123. perf_output_end(&handle);
  5124. out:
  5125. comm_event->event_id.header.size = size;
  5126. }
  5127. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  5128. {
  5129. char comm[TASK_COMM_LEN];
  5130. unsigned int size;
  5131. memset(comm, 0, sizeof(comm));
  5132. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  5133. size = ALIGN(strlen(comm)+1, sizeof(u64));
  5134. comm_event->comm = comm;
  5135. comm_event->comm_size = size;
  5136. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  5137. perf_event_aux(perf_event_comm_output,
  5138. comm_event,
  5139. NULL);
  5140. }
  5141. void perf_event_comm(struct task_struct *task, bool exec)
  5142. {
  5143. struct perf_comm_event comm_event;
  5144. if (!atomic_read(&nr_comm_events))
  5145. return;
  5146. comm_event = (struct perf_comm_event){
  5147. .task = task,
  5148. /* .comm */
  5149. /* .comm_size */
  5150. .event_id = {
  5151. .header = {
  5152. .type = PERF_RECORD_COMM,
  5153. .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
  5154. /* .size */
  5155. },
  5156. /* .pid */
  5157. /* .tid */
  5158. },
  5159. };
  5160. perf_event_comm_event(&comm_event);
  5161. }
  5162. /*
  5163. * mmap tracking
  5164. */
  5165. struct perf_mmap_event {
  5166. struct vm_area_struct *vma;
  5167. const char *file_name;
  5168. int file_size;
  5169. int maj, min;
  5170. u64 ino;
  5171. u64 ino_generation;
  5172. u32 prot, flags;
  5173. struct {
  5174. struct perf_event_header header;
  5175. u32 pid;
  5176. u32 tid;
  5177. u64 start;
  5178. u64 len;
  5179. u64 pgoff;
  5180. } event_id;
  5181. };
  5182. static int perf_event_mmap_match(struct perf_event *event,
  5183. void *data)
  5184. {
  5185. struct perf_mmap_event *mmap_event = data;
  5186. struct vm_area_struct *vma = mmap_event->vma;
  5187. int executable = vma->vm_flags & VM_EXEC;
  5188. return (!executable && event->attr.mmap_data) ||
  5189. (executable && (event->attr.mmap || event->attr.mmap2));
  5190. }
  5191. static void perf_event_mmap_output(struct perf_event *event,
  5192. void *data)
  5193. {
  5194. struct perf_mmap_event *mmap_event = data;
  5195. struct perf_output_handle handle;
  5196. struct perf_sample_data sample;
  5197. int size = mmap_event->event_id.header.size;
  5198. int ret;
  5199. if (!perf_event_mmap_match(event, data))
  5200. return;
  5201. if (event->attr.mmap2) {
  5202. mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
  5203. mmap_event->event_id.header.size += sizeof(mmap_event->maj);
  5204. mmap_event->event_id.header.size += sizeof(mmap_event->min);
  5205. mmap_event->event_id.header.size += sizeof(mmap_event->ino);
  5206. mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
  5207. mmap_event->event_id.header.size += sizeof(mmap_event->prot);
  5208. mmap_event->event_id.header.size += sizeof(mmap_event->flags);
  5209. }
  5210. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  5211. ret = perf_output_begin(&handle, event,
  5212. mmap_event->event_id.header.size);
  5213. if (ret)
  5214. goto out;
  5215. mmap_event->event_id.pid = perf_event_pid(event, current);
  5216. mmap_event->event_id.tid = perf_event_tid(event, current);
  5217. perf_output_put(&handle, mmap_event->event_id);
  5218. if (event->attr.mmap2) {
  5219. perf_output_put(&handle, mmap_event->maj);
  5220. perf_output_put(&handle, mmap_event->min);
  5221. perf_output_put(&handle, mmap_event->ino);
  5222. perf_output_put(&handle, mmap_event->ino_generation);
  5223. perf_output_put(&handle, mmap_event->prot);
  5224. perf_output_put(&handle, mmap_event->flags);
  5225. }
  5226. __output_copy(&handle, mmap_event->file_name,
  5227. mmap_event->file_size);
  5228. perf_event__output_id_sample(event, &handle, &sample);
  5229. perf_output_end(&handle);
  5230. out:
  5231. mmap_event->event_id.header.size = size;
  5232. }
  5233. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  5234. {
  5235. struct vm_area_struct *vma = mmap_event->vma;
  5236. struct file *file = vma->vm_file;
  5237. int maj = 0, min = 0;
  5238. u64 ino = 0, gen = 0;
  5239. u32 prot = 0, flags = 0;
  5240. unsigned int size;
  5241. char tmp[16];
  5242. char *buf = NULL;
  5243. char *name;
  5244. if (file) {
  5245. struct inode *inode;
  5246. dev_t dev;
  5247. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  5248. if (!buf) {
  5249. name = "//enomem";
  5250. goto cpy_name;
  5251. }
  5252. /*
  5253. * d_path() works from the end of the rb backwards, so we
  5254. * need to add enough zero bytes after the string to handle
  5255. * the 64bit alignment we do later.
  5256. */
  5257. name = file_path(file, buf, PATH_MAX - sizeof(u64));
  5258. if (IS_ERR(name)) {
  5259. name = "//toolong";
  5260. goto cpy_name;
  5261. }
  5262. inode = file_inode(vma->vm_file);
  5263. dev = inode->i_sb->s_dev;
  5264. ino = inode->i_ino;
  5265. gen = inode->i_generation;
  5266. maj = MAJOR(dev);
  5267. min = MINOR(dev);
  5268. if (vma->vm_flags & VM_READ)
  5269. prot |= PROT_READ;
  5270. if (vma->vm_flags & VM_WRITE)
  5271. prot |= PROT_WRITE;
  5272. if (vma->vm_flags & VM_EXEC)
  5273. prot |= PROT_EXEC;
  5274. if (vma->vm_flags & VM_MAYSHARE)
  5275. flags = MAP_SHARED;
  5276. else
  5277. flags = MAP_PRIVATE;
  5278. if (vma->vm_flags & VM_DENYWRITE)
  5279. flags |= MAP_DENYWRITE;
  5280. if (vma->vm_flags & VM_MAYEXEC)
  5281. flags |= MAP_EXECUTABLE;
  5282. if (vma->vm_flags & VM_LOCKED)
  5283. flags |= MAP_LOCKED;
  5284. if (vma->vm_flags & VM_HUGETLB)
  5285. flags |= MAP_HUGETLB;
  5286. goto got_name;
  5287. } else {
  5288. if (vma->vm_ops && vma->vm_ops->name) {
  5289. name = (char *) vma->vm_ops->name(vma);
  5290. if (name)
  5291. goto cpy_name;
  5292. }
  5293. name = (char *)arch_vma_name(vma);
  5294. if (name)
  5295. goto cpy_name;
  5296. if (vma->vm_start <= vma->vm_mm->start_brk &&
  5297. vma->vm_end >= vma->vm_mm->brk) {
  5298. name = "[heap]";
  5299. goto cpy_name;
  5300. }
  5301. if (vma->vm_start <= vma->vm_mm->start_stack &&
  5302. vma->vm_end >= vma->vm_mm->start_stack) {
  5303. name = "[stack]";
  5304. goto cpy_name;
  5305. }
  5306. name = "//anon";
  5307. goto cpy_name;
  5308. }
  5309. cpy_name:
  5310. strlcpy(tmp, name, sizeof(tmp));
  5311. name = tmp;
  5312. got_name:
  5313. /*
  5314. * Since our buffer works in 8 byte units we need to align our string
  5315. * size to a multiple of 8. However, we must guarantee the tail end is
  5316. * zero'd out to avoid leaking random bits to userspace.
  5317. */
  5318. size = strlen(name)+1;
  5319. while (!IS_ALIGNED(size, sizeof(u64)))
  5320. name[size++] = '\0';
  5321. mmap_event->file_name = name;
  5322. mmap_event->file_size = size;
  5323. mmap_event->maj = maj;
  5324. mmap_event->min = min;
  5325. mmap_event->ino = ino;
  5326. mmap_event->ino_generation = gen;
  5327. mmap_event->prot = prot;
  5328. mmap_event->flags = flags;
  5329. if (!(vma->vm_flags & VM_EXEC))
  5330. mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  5331. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  5332. perf_event_aux(perf_event_mmap_output,
  5333. mmap_event,
  5334. NULL);
  5335. kfree(buf);
  5336. }
  5337. /*
  5338. * Whether this @filter depends on a dynamic object which is not loaded
  5339. * yet or its load addresses are not known.
  5340. */
  5341. static bool perf_addr_filter_needs_mmap(struct perf_addr_filter *filter)
  5342. {
  5343. return filter->filter && filter->inode;
  5344. }
  5345. /*
  5346. * Check whether inode and address range match filter criteria.
  5347. */
  5348. static bool perf_addr_filter_match(struct perf_addr_filter *filter,
  5349. struct file *file, unsigned long offset,
  5350. unsigned long size)
  5351. {
  5352. if (filter->inode != file->f_inode)
  5353. return false;
  5354. if (filter->offset > offset + size)
  5355. return false;
  5356. if (filter->offset + filter->size < offset)
  5357. return false;
  5358. return true;
  5359. }
  5360. static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
  5361. {
  5362. struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
  5363. struct vm_area_struct *vma = data;
  5364. unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
  5365. struct file *file = vma->vm_file;
  5366. struct perf_addr_filter *filter;
  5367. unsigned int restart = 0, count = 0;
  5368. if (!has_addr_filter(event))
  5369. return;
  5370. if (!file)
  5371. return;
  5372. raw_spin_lock_irqsave(&ifh->lock, flags);
  5373. list_for_each_entry(filter, &ifh->list, entry) {
  5374. if (perf_addr_filter_match(filter, file, off,
  5375. vma->vm_end - vma->vm_start)) {
  5376. event->addr_filters_offs[count] = vma->vm_start;
  5377. restart++;
  5378. }
  5379. count++;
  5380. }
  5381. if (restart)
  5382. event->addr_filters_gen++;
  5383. raw_spin_unlock_irqrestore(&ifh->lock, flags);
  5384. if (restart)
  5385. perf_event_restart(event);
  5386. }
  5387. /*
  5388. * Adjust all task's events' filters to the new vma
  5389. */
  5390. static void perf_addr_filters_adjust(struct vm_area_struct *vma)
  5391. {
  5392. struct perf_event_context *ctx;
  5393. int ctxn;
  5394. rcu_read_lock();
  5395. for_each_task_context_nr(ctxn) {
  5396. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  5397. if (!ctx)
  5398. continue;
  5399. perf_event_aux_ctx(ctx, __perf_addr_filters_adjust, vma, true);
  5400. }
  5401. rcu_read_unlock();
  5402. }
  5403. void perf_event_mmap(struct vm_area_struct *vma)
  5404. {
  5405. struct perf_mmap_event mmap_event;
  5406. if (!atomic_read(&nr_mmap_events))
  5407. return;
  5408. mmap_event = (struct perf_mmap_event){
  5409. .vma = vma,
  5410. /* .file_name */
  5411. /* .file_size */
  5412. .event_id = {
  5413. .header = {
  5414. .type = PERF_RECORD_MMAP,
  5415. .misc = PERF_RECORD_MISC_USER,
  5416. /* .size */
  5417. },
  5418. /* .pid */
  5419. /* .tid */
  5420. .start = vma->vm_start,
  5421. .len = vma->vm_end - vma->vm_start,
  5422. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  5423. },
  5424. /* .maj (attr_mmap2 only) */
  5425. /* .min (attr_mmap2 only) */
  5426. /* .ino (attr_mmap2 only) */
  5427. /* .ino_generation (attr_mmap2 only) */
  5428. /* .prot (attr_mmap2 only) */
  5429. /* .flags (attr_mmap2 only) */
  5430. };
  5431. perf_addr_filters_adjust(vma);
  5432. perf_event_mmap_event(&mmap_event);
  5433. }
  5434. void perf_event_aux_event(struct perf_event *event, unsigned long head,
  5435. unsigned long size, u64 flags)
  5436. {
  5437. struct perf_output_handle handle;
  5438. struct perf_sample_data sample;
  5439. struct perf_aux_event {
  5440. struct perf_event_header header;
  5441. u64 offset;
  5442. u64 size;
  5443. u64 flags;
  5444. } rec = {
  5445. .header = {
  5446. .type = PERF_RECORD_AUX,
  5447. .misc = 0,
  5448. .size = sizeof(rec),
  5449. },
  5450. .offset = head,
  5451. .size = size,
  5452. .flags = flags,
  5453. };
  5454. int ret;
  5455. perf_event_header__init_id(&rec.header, &sample, event);
  5456. ret = perf_output_begin(&handle, event, rec.header.size);
  5457. if (ret)
  5458. return;
  5459. perf_output_put(&handle, rec);
  5460. perf_event__output_id_sample(event, &handle, &sample);
  5461. perf_output_end(&handle);
  5462. }
  5463. /*
  5464. * Lost/dropped samples logging
  5465. */
  5466. void perf_log_lost_samples(struct perf_event *event, u64 lost)
  5467. {
  5468. struct perf_output_handle handle;
  5469. struct perf_sample_data sample;
  5470. int ret;
  5471. struct {
  5472. struct perf_event_header header;
  5473. u64 lost;
  5474. } lost_samples_event = {
  5475. .header = {
  5476. .type = PERF_RECORD_LOST_SAMPLES,
  5477. .misc = 0,
  5478. .size = sizeof(lost_samples_event),
  5479. },
  5480. .lost = lost,
  5481. };
  5482. perf_event_header__init_id(&lost_samples_event.header, &sample, event);
  5483. ret = perf_output_begin(&handle, event,
  5484. lost_samples_event.header.size);
  5485. if (ret)
  5486. return;
  5487. perf_output_put(&handle, lost_samples_event);
  5488. perf_event__output_id_sample(event, &handle, &sample);
  5489. perf_output_end(&handle);
  5490. }
  5491. /*
  5492. * context_switch tracking
  5493. */
  5494. struct perf_switch_event {
  5495. struct task_struct *task;
  5496. struct task_struct *next_prev;
  5497. struct {
  5498. struct perf_event_header header;
  5499. u32 next_prev_pid;
  5500. u32 next_prev_tid;
  5501. } event_id;
  5502. };
  5503. static int perf_event_switch_match(struct perf_event *event)
  5504. {
  5505. return event->attr.context_switch;
  5506. }
  5507. static void perf_event_switch_output(struct perf_event *event, void *data)
  5508. {
  5509. struct perf_switch_event *se = data;
  5510. struct perf_output_handle handle;
  5511. struct perf_sample_data sample;
  5512. int ret;
  5513. if (!perf_event_switch_match(event))
  5514. return;
  5515. /* Only CPU-wide events are allowed to see next/prev pid/tid */
  5516. if (event->ctx->task) {
  5517. se->event_id.header.type = PERF_RECORD_SWITCH;
  5518. se->event_id.header.size = sizeof(se->event_id.header);
  5519. } else {
  5520. se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
  5521. se->event_id.header.size = sizeof(se->event_id);
  5522. se->event_id.next_prev_pid =
  5523. perf_event_pid(event, se->next_prev);
  5524. se->event_id.next_prev_tid =
  5525. perf_event_tid(event, se->next_prev);
  5526. }
  5527. perf_event_header__init_id(&se->event_id.header, &sample, event);
  5528. ret = perf_output_begin(&handle, event, se->event_id.header.size);
  5529. if (ret)
  5530. return;
  5531. if (event->ctx->task)
  5532. perf_output_put(&handle, se->event_id.header);
  5533. else
  5534. perf_output_put(&handle, se->event_id);
  5535. perf_event__output_id_sample(event, &handle, &sample);
  5536. perf_output_end(&handle);
  5537. }
  5538. static void perf_event_switch(struct task_struct *task,
  5539. struct task_struct *next_prev, bool sched_in)
  5540. {
  5541. struct perf_switch_event switch_event;
  5542. /* N.B. caller checks nr_switch_events != 0 */
  5543. switch_event = (struct perf_switch_event){
  5544. .task = task,
  5545. .next_prev = next_prev,
  5546. .event_id = {
  5547. .header = {
  5548. /* .type */
  5549. .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
  5550. /* .size */
  5551. },
  5552. /* .next_prev_pid */
  5553. /* .next_prev_tid */
  5554. },
  5555. };
  5556. perf_event_aux(perf_event_switch_output,
  5557. &switch_event,
  5558. NULL);
  5559. }
  5560. /*
  5561. * IRQ throttle logging
  5562. */
  5563. static void perf_log_throttle(struct perf_event *event, int enable)
  5564. {
  5565. struct perf_output_handle handle;
  5566. struct perf_sample_data sample;
  5567. int ret;
  5568. struct {
  5569. struct perf_event_header header;
  5570. u64 time;
  5571. u64 id;
  5572. u64 stream_id;
  5573. } throttle_event = {
  5574. .header = {
  5575. .type = PERF_RECORD_THROTTLE,
  5576. .misc = 0,
  5577. .size = sizeof(throttle_event),
  5578. },
  5579. .time = perf_event_clock(event),
  5580. .id = primary_event_id(event),
  5581. .stream_id = event->id,
  5582. };
  5583. if (enable)
  5584. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  5585. perf_event_header__init_id(&throttle_event.header, &sample, event);
  5586. ret = perf_output_begin(&handle, event,
  5587. throttle_event.header.size);
  5588. if (ret)
  5589. return;
  5590. perf_output_put(&handle, throttle_event);
  5591. perf_event__output_id_sample(event, &handle, &sample);
  5592. perf_output_end(&handle);
  5593. }
  5594. static void perf_log_itrace_start(struct perf_event *event)
  5595. {
  5596. struct perf_output_handle handle;
  5597. struct perf_sample_data sample;
  5598. struct perf_aux_event {
  5599. struct perf_event_header header;
  5600. u32 pid;
  5601. u32 tid;
  5602. } rec;
  5603. int ret;
  5604. if (event->parent)
  5605. event = event->parent;
  5606. if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
  5607. event->hw.itrace_started)
  5608. return;
  5609. rec.header.type = PERF_RECORD_ITRACE_START;
  5610. rec.header.misc = 0;
  5611. rec.header.size = sizeof(rec);
  5612. rec.pid = perf_event_pid(event, current);
  5613. rec.tid = perf_event_tid(event, current);
  5614. perf_event_header__init_id(&rec.header, &sample, event);
  5615. ret = perf_output_begin(&handle, event, rec.header.size);
  5616. if (ret)
  5617. return;
  5618. perf_output_put(&handle, rec);
  5619. perf_event__output_id_sample(event, &handle, &sample);
  5620. perf_output_end(&handle);
  5621. }
  5622. /*
  5623. * Generic event overflow handling, sampling.
  5624. */
  5625. static int __perf_event_overflow(struct perf_event *event,
  5626. int throttle, struct perf_sample_data *data,
  5627. struct pt_regs *regs)
  5628. {
  5629. int events = atomic_read(&event->event_limit);
  5630. struct hw_perf_event *hwc = &event->hw;
  5631. u64 seq;
  5632. int ret = 0;
  5633. /*
  5634. * Non-sampling counters might still use the PMI to fold short
  5635. * hardware counters, ignore those.
  5636. */
  5637. if (unlikely(!is_sampling_event(event)))
  5638. return 0;
  5639. seq = __this_cpu_read(perf_throttled_seq);
  5640. if (seq != hwc->interrupts_seq) {
  5641. hwc->interrupts_seq = seq;
  5642. hwc->interrupts = 1;
  5643. } else {
  5644. hwc->interrupts++;
  5645. if (unlikely(throttle
  5646. && hwc->interrupts >= max_samples_per_tick)) {
  5647. __this_cpu_inc(perf_throttled_count);
  5648. tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
  5649. hwc->interrupts = MAX_INTERRUPTS;
  5650. perf_log_throttle(event, 0);
  5651. ret = 1;
  5652. }
  5653. }
  5654. if (event->attr.freq) {
  5655. u64 now = perf_clock();
  5656. s64 delta = now - hwc->freq_time_stamp;
  5657. hwc->freq_time_stamp = now;
  5658. if (delta > 0 && delta < 2*TICK_NSEC)
  5659. perf_adjust_period(event, delta, hwc->last_period, true);
  5660. }
  5661. /*
  5662. * XXX event_limit might not quite work as expected on inherited
  5663. * events
  5664. */
  5665. event->pending_kill = POLL_IN;
  5666. if (events && atomic_dec_and_test(&event->event_limit)) {
  5667. ret = 1;
  5668. event->pending_kill = POLL_HUP;
  5669. event->pending_disable = 1;
  5670. irq_work_queue(&event->pending);
  5671. }
  5672. event->overflow_handler(event, data, regs);
  5673. if (*perf_event_fasync(event) && event->pending_kill) {
  5674. event->pending_wakeup = 1;
  5675. irq_work_queue(&event->pending);
  5676. }
  5677. return ret;
  5678. }
  5679. int perf_event_overflow(struct perf_event *event,
  5680. struct perf_sample_data *data,
  5681. struct pt_regs *regs)
  5682. {
  5683. return __perf_event_overflow(event, 1, data, regs);
  5684. }
  5685. /*
  5686. * Generic software event infrastructure
  5687. */
  5688. struct swevent_htable {
  5689. struct swevent_hlist *swevent_hlist;
  5690. struct mutex hlist_mutex;
  5691. int hlist_refcount;
  5692. /* Recursion avoidance in each contexts */
  5693. int recursion[PERF_NR_CONTEXTS];
  5694. };
  5695. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  5696. /*
  5697. * We directly increment event->count and keep a second value in
  5698. * event->hw.period_left to count intervals. This period event
  5699. * is kept in the range [-sample_period, 0] so that we can use the
  5700. * sign as trigger.
  5701. */
  5702. u64 perf_swevent_set_period(struct perf_event *event)
  5703. {
  5704. struct hw_perf_event *hwc = &event->hw;
  5705. u64 period = hwc->last_period;
  5706. u64 nr, offset;
  5707. s64 old, val;
  5708. hwc->last_period = hwc->sample_period;
  5709. again:
  5710. old = val = local64_read(&hwc->period_left);
  5711. if (val < 0)
  5712. return 0;
  5713. nr = div64_u64(period + val, period);
  5714. offset = nr * period;
  5715. val -= offset;
  5716. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  5717. goto again;
  5718. return nr;
  5719. }
  5720. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  5721. struct perf_sample_data *data,
  5722. struct pt_regs *regs)
  5723. {
  5724. struct hw_perf_event *hwc = &event->hw;
  5725. int throttle = 0;
  5726. if (!overflow)
  5727. overflow = perf_swevent_set_period(event);
  5728. if (hwc->interrupts == MAX_INTERRUPTS)
  5729. return;
  5730. for (; overflow; overflow--) {
  5731. if (__perf_event_overflow(event, throttle,
  5732. data, regs)) {
  5733. /*
  5734. * We inhibit the overflow from happening when
  5735. * hwc->interrupts == MAX_INTERRUPTS.
  5736. */
  5737. break;
  5738. }
  5739. throttle = 1;
  5740. }
  5741. }
  5742. static void perf_swevent_event(struct perf_event *event, u64 nr,
  5743. struct perf_sample_data *data,
  5744. struct pt_regs *regs)
  5745. {
  5746. struct hw_perf_event *hwc = &event->hw;
  5747. local64_add(nr, &event->count);
  5748. if (!regs)
  5749. return;
  5750. if (!is_sampling_event(event))
  5751. return;
  5752. if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
  5753. data->period = nr;
  5754. return perf_swevent_overflow(event, 1, data, regs);
  5755. } else
  5756. data->period = event->hw.last_period;
  5757. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  5758. return perf_swevent_overflow(event, 1, data, regs);
  5759. if (local64_add_negative(nr, &hwc->period_left))
  5760. return;
  5761. perf_swevent_overflow(event, 0, data, regs);
  5762. }
  5763. static int perf_exclude_event(struct perf_event *event,
  5764. struct pt_regs *regs)
  5765. {
  5766. if (event->hw.state & PERF_HES_STOPPED)
  5767. return 1;
  5768. if (regs) {
  5769. if (event->attr.exclude_user && user_mode(regs))
  5770. return 1;
  5771. if (event->attr.exclude_kernel && !user_mode(regs))
  5772. return 1;
  5773. }
  5774. return 0;
  5775. }
  5776. static int perf_swevent_match(struct perf_event *event,
  5777. enum perf_type_id type,
  5778. u32 event_id,
  5779. struct perf_sample_data *data,
  5780. struct pt_regs *regs)
  5781. {
  5782. if (event->attr.type != type)
  5783. return 0;
  5784. if (event->attr.config != event_id)
  5785. return 0;
  5786. if (perf_exclude_event(event, regs))
  5787. return 0;
  5788. return 1;
  5789. }
  5790. static inline u64 swevent_hash(u64 type, u32 event_id)
  5791. {
  5792. u64 val = event_id | (type << 32);
  5793. return hash_64(val, SWEVENT_HLIST_BITS);
  5794. }
  5795. static inline struct hlist_head *
  5796. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  5797. {
  5798. u64 hash = swevent_hash(type, event_id);
  5799. return &hlist->heads[hash];
  5800. }
  5801. /* For the read side: events when they trigger */
  5802. static inline struct hlist_head *
  5803. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  5804. {
  5805. struct swevent_hlist *hlist;
  5806. hlist = rcu_dereference(swhash->swevent_hlist);
  5807. if (!hlist)
  5808. return NULL;
  5809. return __find_swevent_head(hlist, type, event_id);
  5810. }
  5811. /* For the event head insertion and removal in the hlist */
  5812. static inline struct hlist_head *
  5813. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  5814. {
  5815. struct swevent_hlist *hlist;
  5816. u32 event_id = event->attr.config;
  5817. u64 type = event->attr.type;
  5818. /*
  5819. * Event scheduling is always serialized against hlist allocation
  5820. * and release. Which makes the protected version suitable here.
  5821. * The context lock guarantees that.
  5822. */
  5823. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  5824. lockdep_is_held(&event->ctx->lock));
  5825. if (!hlist)
  5826. return NULL;
  5827. return __find_swevent_head(hlist, type, event_id);
  5828. }
  5829. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  5830. u64 nr,
  5831. struct perf_sample_data *data,
  5832. struct pt_regs *regs)
  5833. {
  5834. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5835. struct perf_event *event;
  5836. struct hlist_head *head;
  5837. rcu_read_lock();
  5838. head = find_swevent_head_rcu(swhash, type, event_id);
  5839. if (!head)
  5840. goto end;
  5841. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  5842. if (perf_swevent_match(event, type, event_id, data, regs))
  5843. perf_swevent_event(event, nr, data, regs);
  5844. }
  5845. end:
  5846. rcu_read_unlock();
  5847. }
  5848. DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
  5849. int perf_swevent_get_recursion_context(void)
  5850. {
  5851. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5852. return get_recursion_context(swhash->recursion);
  5853. }
  5854. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  5855. void perf_swevent_put_recursion_context(int rctx)
  5856. {
  5857. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5858. put_recursion_context(swhash->recursion, rctx);
  5859. }
  5860. void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  5861. {
  5862. struct perf_sample_data data;
  5863. if (WARN_ON_ONCE(!regs))
  5864. return;
  5865. perf_sample_data_init(&data, addr, 0);
  5866. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
  5867. }
  5868. void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  5869. {
  5870. int rctx;
  5871. preempt_disable_notrace();
  5872. rctx = perf_swevent_get_recursion_context();
  5873. if (unlikely(rctx < 0))
  5874. goto fail;
  5875. ___perf_sw_event(event_id, nr, regs, addr);
  5876. perf_swevent_put_recursion_context(rctx);
  5877. fail:
  5878. preempt_enable_notrace();
  5879. }
  5880. static void perf_swevent_read(struct perf_event *event)
  5881. {
  5882. }
  5883. static int perf_swevent_add(struct perf_event *event, int flags)
  5884. {
  5885. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5886. struct hw_perf_event *hwc = &event->hw;
  5887. struct hlist_head *head;
  5888. if (is_sampling_event(event)) {
  5889. hwc->last_period = hwc->sample_period;
  5890. perf_swevent_set_period(event);
  5891. }
  5892. hwc->state = !(flags & PERF_EF_START);
  5893. head = find_swevent_head(swhash, event);
  5894. if (WARN_ON_ONCE(!head))
  5895. return -EINVAL;
  5896. hlist_add_head_rcu(&event->hlist_entry, head);
  5897. perf_event_update_userpage(event);
  5898. return 0;
  5899. }
  5900. static void perf_swevent_del(struct perf_event *event, int flags)
  5901. {
  5902. hlist_del_rcu(&event->hlist_entry);
  5903. }
  5904. static void perf_swevent_start(struct perf_event *event, int flags)
  5905. {
  5906. event->hw.state = 0;
  5907. }
  5908. static void perf_swevent_stop(struct perf_event *event, int flags)
  5909. {
  5910. event->hw.state = PERF_HES_STOPPED;
  5911. }
  5912. /* Deref the hlist from the update side */
  5913. static inline struct swevent_hlist *
  5914. swevent_hlist_deref(struct swevent_htable *swhash)
  5915. {
  5916. return rcu_dereference_protected(swhash->swevent_hlist,
  5917. lockdep_is_held(&swhash->hlist_mutex));
  5918. }
  5919. static void swevent_hlist_release(struct swevent_htable *swhash)
  5920. {
  5921. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  5922. if (!hlist)
  5923. return;
  5924. RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
  5925. kfree_rcu(hlist, rcu_head);
  5926. }
  5927. static void swevent_hlist_put_cpu(int cpu)
  5928. {
  5929. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5930. mutex_lock(&swhash->hlist_mutex);
  5931. if (!--swhash->hlist_refcount)
  5932. swevent_hlist_release(swhash);
  5933. mutex_unlock(&swhash->hlist_mutex);
  5934. }
  5935. static void swevent_hlist_put(void)
  5936. {
  5937. int cpu;
  5938. for_each_possible_cpu(cpu)
  5939. swevent_hlist_put_cpu(cpu);
  5940. }
  5941. static int swevent_hlist_get_cpu(int cpu)
  5942. {
  5943. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5944. int err = 0;
  5945. mutex_lock(&swhash->hlist_mutex);
  5946. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  5947. struct swevent_hlist *hlist;
  5948. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  5949. if (!hlist) {
  5950. err = -ENOMEM;
  5951. goto exit;
  5952. }
  5953. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5954. }
  5955. swhash->hlist_refcount++;
  5956. exit:
  5957. mutex_unlock(&swhash->hlist_mutex);
  5958. return err;
  5959. }
  5960. static int swevent_hlist_get(void)
  5961. {
  5962. int err, cpu, failed_cpu;
  5963. get_online_cpus();
  5964. for_each_possible_cpu(cpu) {
  5965. err = swevent_hlist_get_cpu(cpu);
  5966. if (err) {
  5967. failed_cpu = cpu;
  5968. goto fail;
  5969. }
  5970. }
  5971. put_online_cpus();
  5972. return 0;
  5973. fail:
  5974. for_each_possible_cpu(cpu) {
  5975. if (cpu == failed_cpu)
  5976. break;
  5977. swevent_hlist_put_cpu(cpu);
  5978. }
  5979. put_online_cpus();
  5980. return err;
  5981. }
  5982. struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  5983. static void sw_perf_event_destroy(struct perf_event *event)
  5984. {
  5985. u64 event_id = event->attr.config;
  5986. WARN_ON(event->parent);
  5987. static_key_slow_dec(&perf_swevent_enabled[event_id]);
  5988. swevent_hlist_put();
  5989. }
  5990. static int perf_swevent_init(struct perf_event *event)
  5991. {
  5992. u64 event_id = event->attr.config;
  5993. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5994. return -ENOENT;
  5995. /*
  5996. * no branch sampling for software events
  5997. */
  5998. if (has_branch_stack(event))
  5999. return -EOPNOTSUPP;
  6000. switch (event_id) {
  6001. case PERF_COUNT_SW_CPU_CLOCK:
  6002. case PERF_COUNT_SW_TASK_CLOCK:
  6003. return -ENOENT;
  6004. default:
  6005. break;
  6006. }
  6007. if (event_id >= PERF_COUNT_SW_MAX)
  6008. return -ENOENT;
  6009. if (!event->parent) {
  6010. int err;
  6011. err = swevent_hlist_get();
  6012. if (err)
  6013. return err;
  6014. static_key_slow_inc(&perf_swevent_enabled[event_id]);
  6015. event->destroy = sw_perf_event_destroy;
  6016. }
  6017. return 0;
  6018. }
  6019. static struct pmu perf_swevent = {
  6020. .task_ctx_nr = perf_sw_context,
  6021. .capabilities = PERF_PMU_CAP_NO_NMI,
  6022. .event_init = perf_swevent_init,
  6023. .add = perf_swevent_add,
  6024. .del = perf_swevent_del,
  6025. .start = perf_swevent_start,
  6026. .stop = perf_swevent_stop,
  6027. .read = perf_swevent_read,
  6028. };
  6029. #ifdef CONFIG_EVENT_TRACING
  6030. static int perf_tp_filter_match(struct perf_event *event,
  6031. struct perf_sample_data *data)
  6032. {
  6033. void *record = data->raw->data;
  6034. /* only top level events have filters set */
  6035. if (event->parent)
  6036. event = event->parent;
  6037. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  6038. return 1;
  6039. return 0;
  6040. }
  6041. static int perf_tp_event_match(struct perf_event *event,
  6042. struct perf_sample_data *data,
  6043. struct pt_regs *regs)
  6044. {
  6045. if (event->hw.state & PERF_HES_STOPPED)
  6046. return 0;
  6047. /*
  6048. * All tracepoints are from kernel-space.
  6049. */
  6050. if (event->attr.exclude_kernel)
  6051. return 0;
  6052. if (!perf_tp_filter_match(event, data))
  6053. return 0;
  6054. return 1;
  6055. }
  6056. void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
  6057. struct trace_event_call *call, u64 count,
  6058. struct pt_regs *regs, struct hlist_head *head,
  6059. struct task_struct *task)
  6060. {
  6061. struct bpf_prog *prog = call->prog;
  6062. if (prog) {
  6063. *(struct pt_regs **)raw_data = regs;
  6064. if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
  6065. perf_swevent_put_recursion_context(rctx);
  6066. return;
  6067. }
  6068. }
  6069. perf_tp_event(call->event.type, count, raw_data, size, regs, head,
  6070. rctx, task);
  6071. }
  6072. EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
  6073. void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
  6074. struct pt_regs *regs, struct hlist_head *head, int rctx,
  6075. struct task_struct *task)
  6076. {
  6077. struct perf_sample_data data;
  6078. struct perf_event *event;
  6079. struct perf_raw_record raw = {
  6080. .size = entry_size,
  6081. .data = record,
  6082. };
  6083. perf_sample_data_init(&data, 0, 0);
  6084. data.raw = &raw;
  6085. perf_trace_buf_update(record, event_type);
  6086. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  6087. if (perf_tp_event_match(event, &data, regs))
  6088. perf_swevent_event(event, count, &data, regs);
  6089. }
  6090. /*
  6091. * If we got specified a target task, also iterate its context and
  6092. * deliver this event there too.
  6093. */
  6094. if (task && task != current) {
  6095. struct perf_event_context *ctx;
  6096. struct trace_entry *entry = record;
  6097. rcu_read_lock();
  6098. ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
  6099. if (!ctx)
  6100. goto unlock;
  6101. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  6102. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  6103. continue;
  6104. if (event->attr.config != entry->type)
  6105. continue;
  6106. if (perf_tp_event_match(event, &data, regs))
  6107. perf_swevent_event(event, count, &data, regs);
  6108. }
  6109. unlock:
  6110. rcu_read_unlock();
  6111. }
  6112. perf_swevent_put_recursion_context(rctx);
  6113. }
  6114. EXPORT_SYMBOL_GPL(perf_tp_event);
  6115. static void tp_perf_event_destroy(struct perf_event *event)
  6116. {
  6117. perf_trace_destroy(event);
  6118. }
  6119. static int perf_tp_event_init(struct perf_event *event)
  6120. {
  6121. int err;
  6122. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  6123. return -ENOENT;
  6124. /*
  6125. * no branch sampling for tracepoint events
  6126. */
  6127. if (has_branch_stack(event))
  6128. return -EOPNOTSUPP;
  6129. err = perf_trace_init(event);
  6130. if (err)
  6131. return err;
  6132. event->destroy = tp_perf_event_destroy;
  6133. return 0;
  6134. }
  6135. static struct pmu perf_tracepoint = {
  6136. .task_ctx_nr = perf_sw_context,
  6137. .event_init = perf_tp_event_init,
  6138. .add = perf_trace_add,
  6139. .del = perf_trace_del,
  6140. .start = perf_swevent_start,
  6141. .stop = perf_swevent_stop,
  6142. .read = perf_swevent_read,
  6143. };
  6144. static inline void perf_tp_register(void)
  6145. {
  6146. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  6147. }
  6148. static void perf_event_free_filter(struct perf_event *event)
  6149. {
  6150. ftrace_profile_free_filter(event);
  6151. }
  6152. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
  6153. {
  6154. bool is_kprobe, is_tracepoint;
  6155. struct bpf_prog *prog;
  6156. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  6157. return -EINVAL;
  6158. if (event->tp_event->prog)
  6159. return -EEXIST;
  6160. is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
  6161. is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
  6162. if (!is_kprobe && !is_tracepoint)
  6163. /* bpf programs can only be attached to u/kprobe or tracepoint */
  6164. return -EINVAL;
  6165. prog = bpf_prog_get(prog_fd);
  6166. if (IS_ERR(prog))
  6167. return PTR_ERR(prog);
  6168. if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
  6169. (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
  6170. /* valid fd, but invalid bpf program type */
  6171. bpf_prog_put(prog);
  6172. return -EINVAL;
  6173. }
  6174. if (is_tracepoint) {
  6175. int off = trace_event_get_offsets(event->tp_event);
  6176. if (prog->aux->max_ctx_offset > off) {
  6177. bpf_prog_put(prog);
  6178. return -EACCES;
  6179. }
  6180. }
  6181. event->tp_event->prog = prog;
  6182. return 0;
  6183. }
  6184. static void perf_event_free_bpf_prog(struct perf_event *event)
  6185. {
  6186. struct bpf_prog *prog;
  6187. if (!event->tp_event)
  6188. return;
  6189. prog = event->tp_event->prog;
  6190. if (prog) {
  6191. event->tp_event->prog = NULL;
  6192. bpf_prog_put(prog);
  6193. }
  6194. }
  6195. #else
  6196. static inline void perf_tp_register(void)
  6197. {
  6198. }
  6199. static void perf_event_free_filter(struct perf_event *event)
  6200. {
  6201. }
  6202. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
  6203. {
  6204. return -ENOENT;
  6205. }
  6206. static void perf_event_free_bpf_prog(struct perf_event *event)
  6207. {
  6208. }
  6209. #endif /* CONFIG_EVENT_TRACING */
  6210. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  6211. void perf_bp_event(struct perf_event *bp, void *data)
  6212. {
  6213. struct perf_sample_data sample;
  6214. struct pt_regs *regs = data;
  6215. perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
  6216. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  6217. perf_swevent_event(bp, 1, &sample, regs);
  6218. }
  6219. #endif
  6220. /*
  6221. * Allocate a new address filter
  6222. */
  6223. static struct perf_addr_filter *
  6224. perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
  6225. {
  6226. int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
  6227. struct perf_addr_filter *filter;
  6228. filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
  6229. if (!filter)
  6230. return NULL;
  6231. INIT_LIST_HEAD(&filter->entry);
  6232. list_add_tail(&filter->entry, filters);
  6233. return filter;
  6234. }
  6235. static void free_filters_list(struct list_head *filters)
  6236. {
  6237. struct perf_addr_filter *filter, *iter;
  6238. list_for_each_entry_safe(filter, iter, filters, entry) {
  6239. if (filter->inode)
  6240. iput(filter->inode);
  6241. list_del(&filter->entry);
  6242. kfree(filter);
  6243. }
  6244. }
  6245. /*
  6246. * Free existing address filters and optionally install new ones
  6247. */
  6248. static void perf_addr_filters_splice(struct perf_event *event,
  6249. struct list_head *head)
  6250. {
  6251. unsigned long flags;
  6252. LIST_HEAD(list);
  6253. if (!has_addr_filter(event))
  6254. return;
  6255. /* don't bother with children, they don't have their own filters */
  6256. if (event->parent)
  6257. return;
  6258. raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
  6259. list_splice_init(&event->addr_filters.list, &list);
  6260. if (head)
  6261. list_splice(head, &event->addr_filters.list);
  6262. raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
  6263. free_filters_list(&list);
  6264. }
  6265. /*
  6266. * Scan through mm's vmas and see if one of them matches the
  6267. * @filter; if so, adjust filter's address range.
  6268. * Called with mm::mmap_sem down for reading.
  6269. */
  6270. static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
  6271. struct mm_struct *mm)
  6272. {
  6273. struct vm_area_struct *vma;
  6274. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  6275. struct file *file = vma->vm_file;
  6276. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  6277. unsigned long vma_size = vma->vm_end - vma->vm_start;
  6278. if (!file)
  6279. continue;
  6280. if (!perf_addr_filter_match(filter, file, off, vma_size))
  6281. continue;
  6282. return vma->vm_start;
  6283. }
  6284. return 0;
  6285. }
  6286. /*
  6287. * Update event's address range filters based on the
  6288. * task's existing mappings, if any.
  6289. */
  6290. static void perf_event_addr_filters_apply(struct perf_event *event)
  6291. {
  6292. struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
  6293. struct task_struct *task = READ_ONCE(event->ctx->task);
  6294. struct perf_addr_filter *filter;
  6295. struct mm_struct *mm = NULL;
  6296. unsigned int count = 0;
  6297. unsigned long flags;
  6298. /*
  6299. * We may observe TASK_TOMBSTONE, which means that the event tear-down
  6300. * will stop on the parent's child_mutex that our caller is also holding
  6301. */
  6302. if (task == TASK_TOMBSTONE)
  6303. return;
  6304. mm = get_task_mm(event->ctx->task);
  6305. if (!mm)
  6306. goto restart;
  6307. down_read(&mm->mmap_sem);
  6308. raw_spin_lock_irqsave(&ifh->lock, flags);
  6309. list_for_each_entry(filter, &ifh->list, entry) {
  6310. event->addr_filters_offs[count] = 0;
  6311. if (perf_addr_filter_needs_mmap(filter))
  6312. event->addr_filters_offs[count] =
  6313. perf_addr_filter_apply(filter, mm);
  6314. count++;
  6315. }
  6316. event->addr_filters_gen++;
  6317. raw_spin_unlock_irqrestore(&ifh->lock, flags);
  6318. up_read(&mm->mmap_sem);
  6319. mmput(mm);
  6320. restart:
  6321. perf_event_restart(event);
  6322. }
  6323. /*
  6324. * Address range filtering: limiting the data to certain
  6325. * instruction address ranges. Filters are ioctl()ed to us from
  6326. * userspace as ascii strings.
  6327. *
  6328. * Filter string format:
  6329. *
  6330. * ACTION RANGE_SPEC
  6331. * where ACTION is one of the
  6332. * * "filter": limit the trace to this region
  6333. * * "start": start tracing from this address
  6334. * * "stop": stop tracing at this address/region;
  6335. * RANGE_SPEC is
  6336. * * for kernel addresses: <start address>[/<size>]
  6337. * * for object files: <start address>[/<size>]@</path/to/object/file>
  6338. *
  6339. * if <size> is not specified, the range is treated as a single address.
  6340. */
  6341. enum {
  6342. IF_ACT_FILTER,
  6343. IF_ACT_START,
  6344. IF_ACT_STOP,
  6345. IF_SRC_FILE,
  6346. IF_SRC_KERNEL,
  6347. IF_SRC_FILEADDR,
  6348. IF_SRC_KERNELADDR,
  6349. };
  6350. enum {
  6351. IF_STATE_ACTION = 0,
  6352. IF_STATE_SOURCE,
  6353. IF_STATE_END,
  6354. };
  6355. static const match_table_t if_tokens = {
  6356. { IF_ACT_FILTER, "filter" },
  6357. { IF_ACT_START, "start" },
  6358. { IF_ACT_STOP, "stop" },
  6359. { IF_SRC_FILE, "%u/%u@%s" },
  6360. { IF_SRC_KERNEL, "%u/%u" },
  6361. { IF_SRC_FILEADDR, "%u@%s" },
  6362. { IF_SRC_KERNELADDR, "%u" },
  6363. };
  6364. /*
  6365. * Address filter string parser
  6366. */
  6367. static int
  6368. perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
  6369. struct list_head *filters)
  6370. {
  6371. struct perf_addr_filter *filter = NULL;
  6372. char *start, *orig, *filename = NULL;
  6373. struct path path;
  6374. substring_t args[MAX_OPT_ARGS];
  6375. int state = IF_STATE_ACTION, token;
  6376. unsigned int kernel = 0;
  6377. int ret = -EINVAL;
  6378. orig = fstr = kstrdup(fstr, GFP_KERNEL);
  6379. if (!fstr)
  6380. return -ENOMEM;
  6381. while ((start = strsep(&fstr, " ,\n")) != NULL) {
  6382. ret = -EINVAL;
  6383. if (!*start)
  6384. continue;
  6385. /* filter definition begins */
  6386. if (state == IF_STATE_ACTION) {
  6387. filter = perf_addr_filter_new(event, filters);
  6388. if (!filter)
  6389. goto fail;
  6390. }
  6391. token = match_token(start, if_tokens, args);
  6392. switch (token) {
  6393. case IF_ACT_FILTER:
  6394. case IF_ACT_START:
  6395. filter->filter = 1;
  6396. case IF_ACT_STOP:
  6397. if (state != IF_STATE_ACTION)
  6398. goto fail;
  6399. state = IF_STATE_SOURCE;
  6400. break;
  6401. case IF_SRC_KERNELADDR:
  6402. case IF_SRC_KERNEL:
  6403. kernel = 1;
  6404. case IF_SRC_FILEADDR:
  6405. case IF_SRC_FILE:
  6406. if (state != IF_STATE_SOURCE)
  6407. goto fail;
  6408. if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
  6409. filter->range = 1;
  6410. *args[0].to = 0;
  6411. ret = kstrtoul(args[0].from, 0, &filter->offset);
  6412. if (ret)
  6413. goto fail;
  6414. if (filter->range) {
  6415. *args[1].to = 0;
  6416. ret = kstrtoul(args[1].from, 0, &filter->size);
  6417. if (ret)
  6418. goto fail;
  6419. }
  6420. if (token == IF_SRC_FILE) {
  6421. filename = match_strdup(&args[2]);
  6422. if (!filename) {
  6423. ret = -ENOMEM;
  6424. goto fail;
  6425. }
  6426. }
  6427. state = IF_STATE_END;
  6428. break;
  6429. default:
  6430. goto fail;
  6431. }
  6432. /*
  6433. * Filter definition is fully parsed, validate and install it.
  6434. * Make sure that it doesn't contradict itself or the event's
  6435. * attribute.
  6436. */
  6437. if (state == IF_STATE_END) {
  6438. if (kernel && event->attr.exclude_kernel)
  6439. goto fail;
  6440. if (!kernel) {
  6441. if (!filename)
  6442. goto fail;
  6443. /* look up the path and grab its inode */
  6444. ret = kern_path(filename, LOOKUP_FOLLOW, &path);
  6445. if (ret)
  6446. goto fail_free_name;
  6447. filter->inode = igrab(d_inode(path.dentry));
  6448. path_put(&path);
  6449. kfree(filename);
  6450. filename = NULL;
  6451. ret = -EINVAL;
  6452. if (!filter->inode ||
  6453. !S_ISREG(filter->inode->i_mode))
  6454. /* free_filters_list() will iput() */
  6455. goto fail;
  6456. }
  6457. /* ready to consume more filters */
  6458. state = IF_STATE_ACTION;
  6459. filter = NULL;
  6460. }
  6461. }
  6462. if (state != IF_STATE_ACTION)
  6463. goto fail;
  6464. kfree(orig);
  6465. return 0;
  6466. fail_free_name:
  6467. kfree(filename);
  6468. fail:
  6469. free_filters_list(filters);
  6470. kfree(orig);
  6471. return ret;
  6472. }
  6473. static int
  6474. perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
  6475. {
  6476. LIST_HEAD(filters);
  6477. int ret;
  6478. /*
  6479. * Since this is called in perf_ioctl() path, we're already holding
  6480. * ctx::mutex.
  6481. */
  6482. lockdep_assert_held(&event->ctx->mutex);
  6483. if (WARN_ON_ONCE(event->parent))
  6484. return -EINVAL;
  6485. /*
  6486. * For now, we only support filtering in per-task events; doing so
  6487. * for CPU-wide events requires additional context switching trickery,
  6488. * since same object code will be mapped at different virtual
  6489. * addresses in different processes.
  6490. */
  6491. if (!event->ctx->task)
  6492. return -EOPNOTSUPP;
  6493. ret = perf_event_parse_addr_filter(event, filter_str, &filters);
  6494. if (ret)
  6495. return ret;
  6496. ret = event->pmu->addr_filters_validate(&filters);
  6497. if (ret) {
  6498. free_filters_list(&filters);
  6499. return ret;
  6500. }
  6501. /* remove existing filters, if any */
  6502. perf_addr_filters_splice(event, &filters);
  6503. /* install new filters */
  6504. perf_event_for_each_child(event, perf_event_addr_filters_apply);
  6505. return ret;
  6506. }
  6507. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  6508. {
  6509. char *filter_str;
  6510. int ret = -EINVAL;
  6511. if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
  6512. !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
  6513. !has_addr_filter(event))
  6514. return -EINVAL;
  6515. filter_str = strndup_user(arg, PAGE_SIZE);
  6516. if (IS_ERR(filter_str))
  6517. return PTR_ERR(filter_str);
  6518. if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
  6519. event->attr.type == PERF_TYPE_TRACEPOINT)
  6520. ret = ftrace_profile_set_filter(event, event->attr.config,
  6521. filter_str);
  6522. else if (has_addr_filter(event))
  6523. ret = perf_event_set_addr_filter(event, filter_str);
  6524. kfree(filter_str);
  6525. return ret;
  6526. }
  6527. /*
  6528. * hrtimer based swevent callback
  6529. */
  6530. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  6531. {
  6532. enum hrtimer_restart ret = HRTIMER_RESTART;
  6533. struct perf_sample_data data;
  6534. struct pt_regs *regs;
  6535. struct perf_event *event;
  6536. u64 period;
  6537. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  6538. if (event->state != PERF_EVENT_STATE_ACTIVE)
  6539. return HRTIMER_NORESTART;
  6540. event->pmu->read(event);
  6541. perf_sample_data_init(&data, 0, event->hw.last_period);
  6542. regs = get_irq_regs();
  6543. if (regs && !perf_exclude_event(event, regs)) {
  6544. if (!(event->attr.exclude_idle && is_idle_task(current)))
  6545. if (__perf_event_overflow(event, 1, &data, regs))
  6546. ret = HRTIMER_NORESTART;
  6547. }
  6548. period = max_t(u64, 10000, event->hw.sample_period);
  6549. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  6550. return ret;
  6551. }
  6552. static void perf_swevent_start_hrtimer(struct perf_event *event)
  6553. {
  6554. struct hw_perf_event *hwc = &event->hw;
  6555. s64 period;
  6556. if (!is_sampling_event(event))
  6557. return;
  6558. period = local64_read(&hwc->period_left);
  6559. if (period) {
  6560. if (period < 0)
  6561. period = 10000;
  6562. local64_set(&hwc->period_left, 0);
  6563. } else {
  6564. period = max_t(u64, 10000, hwc->sample_period);
  6565. }
  6566. hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
  6567. HRTIMER_MODE_REL_PINNED);
  6568. }
  6569. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  6570. {
  6571. struct hw_perf_event *hwc = &event->hw;
  6572. if (is_sampling_event(event)) {
  6573. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  6574. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  6575. hrtimer_cancel(&hwc->hrtimer);
  6576. }
  6577. }
  6578. static void perf_swevent_init_hrtimer(struct perf_event *event)
  6579. {
  6580. struct hw_perf_event *hwc = &event->hw;
  6581. if (!is_sampling_event(event))
  6582. return;
  6583. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  6584. hwc->hrtimer.function = perf_swevent_hrtimer;
  6585. /*
  6586. * Since hrtimers have a fixed rate, we can do a static freq->period
  6587. * mapping and avoid the whole period adjust feedback stuff.
  6588. */
  6589. if (event->attr.freq) {
  6590. long freq = event->attr.sample_freq;
  6591. event->attr.sample_period = NSEC_PER_SEC / freq;
  6592. hwc->sample_period = event->attr.sample_period;
  6593. local64_set(&hwc->period_left, hwc->sample_period);
  6594. hwc->last_period = hwc->sample_period;
  6595. event->attr.freq = 0;
  6596. }
  6597. }
  6598. /*
  6599. * Software event: cpu wall time clock
  6600. */
  6601. static void cpu_clock_event_update(struct perf_event *event)
  6602. {
  6603. s64 prev;
  6604. u64 now;
  6605. now = local_clock();
  6606. prev = local64_xchg(&event->hw.prev_count, now);
  6607. local64_add(now - prev, &event->count);
  6608. }
  6609. static void cpu_clock_event_start(struct perf_event *event, int flags)
  6610. {
  6611. local64_set(&event->hw.prev_count, local_clock());
  6612. perf_swevent_start_hrtimer(event);
  6613. }
  6614. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  6615. {
  6616. perf_swevent_cancel_hrtimer(event);
  6617. cpu_clock_event_update(event);
  6618. }
  6619. static int cpu_clock_event_add(struct perf_event *event, int flags)
  6620. {
  6621. if (flags & PERF_EF_START)
  6622. cpu_clock_event_start(event, flags);
  6623. perf_event_update_userpage(event);
  6624. return 0;
  6625. }
  6626. static void cpu_clock_event_del(struct perf_event *event, int flags)
  6627. {
  6628. cpu_clock_event_stop(event, flags);
  6629. }
  6630. static void cpu_clock_event_read(struct perf_event *event)
  6631. {
  6632. cpu_clock_event_update(event);
  6633. }
  6634. static int cpu_clock_event_init(struct perf_event *event)
  6635. {
  6636. if (event->attr.type != PERF_TYPE_SOFTWARE)
  6637. return -ENOENT;
  6638. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  6639. return -ENOENT;
  6640. /*
  6641. * no branch sampling for software events
  6642. */
  6643. if (has_branch_stack(event))
  6644. return -EOPNOTSUPP;
  6645. perf_swevent_init_hrtimer(event);
  6646. return 0;
  6647. }
  6648. static struct pmu perf_cpu_clock = {
  6649. .task_ctx_nr = perf_sw_context,
  6650. .capabilities = PERF_PMU_CAP_NO_NMI,
  6651. .event_init = cpu_clock_event_init,
  6652. .add = cpu_clock_event_add,
  6653. .del = cpu_clock_event_del,
  6654. .start = cpu_clock_event_start,
  6655. .stop = cpu_clock_event_stop,
  6656. .read = cpu_clock_event_read,
  6657. };
  6658. /*
  6659. * Software event: task time clock
  6660. */
  6661. static void task_clock_event_update(struct perf_event *event, u64 now)
  6662. {
  6663. u64 prev;
  6664. s64 delta;
  6665. prev = local64_xchg(&event->hw.prev_count, now);
  6666. delta = now - prev;
  6667. local64_add(delta, &event->count);
  6668. }
  6669. static void task_clock_event_start(struct perf_event *event, int flags)
  6670. {
  6671. local64_set(&event->hw.prev_count, event->ctx->time);
  6672. perf_swevent_start_hrtimer(event);
  6673. }
  6674. static void task_clock_event_stop(struct perf_event *event, int flags)
  6675. {
  6676. perf_swevent_cancel_hrtimer(event);
  6677. task_clock_event_update(event, event->ctx->time);
  6678. }
  6679. static int task_clock_event_add(struct perf_event *event, int flags)
  6680. {
  6681. if (flags & PERF_EF_START)
  6682. task_clock_event_start(event, flags);
  6683. perf_event_update_userpage(event);
  6684. return 0;
  6685. }
  6686. static void task_clock_event_del(struct perf_event *event, int flags)
  6687. {
  6688. task_clock_event_stop(event, PERF_EF_UPDATE);
  6689. }
  6690. static void task_clock_event_read(struct perf_event *event)
  6691. {
  6692. u64 now = perf_clock();
  6693. u64 delta = now - event->ctx->timestamp;
  6694. u64 time = event->ctx->time + delta;
  6695. task_clock_event_update(event, time);
  6696. }
  6697. static int task_clock_event_init(struct perf_event *event)
  6698. {
  6699. if (event->attr.type != PERF_TYPE_SOFTWARE)
  6700. return -ENOENT;
  6701. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  6702. return -ENOENT;
  6703. /*
  6704. * no branch sampling for software events
  6705. */
  6706. if (has_branch_stack(event))
  6707. return -EOPNOTSUPP;
  6708. perf_swevent_init_hrtimer(event);
  6709. return 0;
  6710. }
  6711. static struct pmu perf_task_clock = {
  6712. .task_ctx_nr = perf_sw_context,
  6713. .capabilities = PERF_PMU_CAP_NO_NMI,
  6714. .event_init = task_clock_event_init,
  6715. .add = task_clock_event_add,
  6716. .del = task_clock_event_del,
  6717. .start = task_clock_event_start,
  6718. .stop = task_clock_event_stop,
  6719. .read = task_clock_event_read,
  6720. };
  6721. static void perf_pmu_nop_void(struct pmu *pmu)
  6722. {
  6723. }
  6724. static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
  6725. {
  6726. }
  6727. static int perf_pmu_nop_int(struct pmu *pmu)
  6728. {
  6729. return 0;
  6730. }
  6731. static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
  6732. static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
  6733. {
  6734. __this_cpu_write(nop_txn_flags, flags);
  6735. if (flags & ~PERF_PMU_TXN_ADD)
  6736. return;
  6737. perf_pmu_disable(pmu);
  6738. }
  6739. static int perf_pmu_commit_txn(struct pmu *pmu)
  6740. {
  6741. unsigned int flags = __this_cpu_read(nop_txn_flags);
  6742. __this_cpu_write(nop_txn_flags, 0);
  6743. if (flags & ~PERF_PMU_TXN_ADD)
  6744. return 0;
  6745. perf_pmu_enable(pmu);
  6746. return 0;
  6747. }
  6748. static void perf_pmu_cancel_txn(struct pmu *pmu)
  6749. {
  6750. unsigned int flags = __this_cpu_read(nop_txn_flags);
  6751. __this_cpu_write(nop_txn_flags, 0);
  6752. if (flags & ~PERF_PMU_TXN_ADD)
  6753. return;
  6754. perf_pmu_enable(pmu);
  6755. }
  6756. static int perf_event_idx_default(struct perf_event *event)
  6757. {
  6758. return 0;
  6759. }
  6760. /*
  6761. * Ensures all contexts with the same task_ctx_nr have the same
  6762. * pmu_cpu_context too.
  6763. */
  6764. static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
  6765. {
  6766. struct pmu *pmu;
  6767. if (ctxn < 0)
  6768. return NULL;
  6769. list_for_each_entry(pmu, &pmus, entry) {
  6770. if (pmu->task_ctx_nr == ctxn)
  6771. return pmu->pmu_cpu_context;
  6772. }
  6773. return NULL;
  6774. }
  6775. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  6776. {
  6777. int cpu;
  6778. for_each_possible_cpu(cpu) {
  6779. struct perf_cpu_context *cpuctx;
  6780. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  6781. if (cpuctx->unique_pmu == old_pmu)
  6782. cpuctx->unique_pmu = pmu;
  6783. }
  6784. }
  6785. static void free_pmu_context(struct pmu *pmu)
  6786. {
  6787. struct pmu *i;
  6788. mutex_lock(&pmus_lock);
  6789. /*
  6790. * Like a real lame refcount.
  6791. */
  6792. list_for_each_entry(i, &pmus, entry) {
  6793. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  6794. update_pmu_context(i, pmu);
  6795. goto out;
  6796. }
  6797. }
  6798. free_percpu(pmu->pmu_cpu_context);
  6799. out:
  6800. mutex_unlock(&pmus_lock);
  6801. }
  6802. /*
  6803. * Let userspace know that this PMU supports address range filtering:
  6804. */
  6805. static ssize_t nr_addr_filters_show(struct device *dev,
  6806. struct device_attribute *attr,
  6807. char *page)
  6808. {
  6809. struct pmu *pmu = dev_get_drvdata(dev);
  6810. return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
  6811. }
  6812. DEVICE_ATTR_RO(nr_addr_filters);
  6813. static struct idr pmu_idr;
  6814. static ssize_t
  6815. type_show(struct device *dev, struct device_attribute *attr, char *page)
  6816. {
  6817. struct pmu *pmu = dev_get_drvdata(dev);
  6818. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  6819. }
  6820. static DEVICE_ATTR_RO(type);
  6821. static ssize_t
  6822. perf_event_mux_interval_ms_show(struct device *dev,
  6823. struct device_attribute *attr,
  6824. char *page)
  6825. {
  6826. struct pmu *pmu = dev_get_drvdata(dev);
  6827. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
  6828. }
  6829. static DEFINE_MUTEX(mux_interval_mutex);
  6830. static ssize_t
  6831. perf_event_mux_interval_ms_store(struct device *dev,
  6832. struct device_attribute *attr,
  6833. const char *buf, size_t count)
  6834. {
  6835. struct pmu *pmu = dev_get_drvdata(dev);
  6836. int timer, cpu, ret;
  6837. ret = kstrtoint(buf, 0, &timer);
  6838. if (ret)
  6839. return ret;
  6840. if (timer < 1)
  6841. return -EINVAL;
  6842. /* same value, noting to do */
  6843. if (timer == pmu->hrtimer_interval_ms)
  6844. return count;
  6845. mutex_lock(&mux_interval_mutex);
  6846. pmu->hrtimer_interval_ms = timer;
  6847. /* update all cpuctx for this PMU */
  6848. get_online_cpus();
  6849. for_each_online_cpu(cpu) {
  6850. struct perf_cpu_context *cpuctx;
  6851. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  6852. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
  6853. cpu_function_call(cpu,
  6854. (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
  6855. }
  6856. put_online_cpus();
  6857. mutex_unlock(&mux_interval_mutex);
  6858. return count;
  6859. }
  6860. static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
  6861. static struct attribute *pmu_dev_attrs[] = {
  6862. &dev_attr_type.attr,
  6863. &dev_attr_perf_event_mux_interval_ms.attr,
  6864. NULL,
  6865. };
  6866. ATTRIBUTE_GROUPS(pmu_dev);
  6867. static int pmu_bus_running;
  6868. static struct bus_type pmu_bus = {
  6869. .name = "event_source",
  6870. .dev_groups = pmu_dev_groups,
  6871. };
  6872. static void pmu_dev_release(struct device *dev)
  6873. {
  6874. kfree(dev);
  6875. }
  6876. static int pmu_dev_alloc(struct pmu *pmu)
  6877. {
  6878. int ret = -ENOMEM;
  6879. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  6880. if (!pmu->dev)
  6881. goto out;
  6882. pmu->dev->groups = pmu->attr_groups;
  6883. device_initialize(pmu->dev);
  6884. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  6885. if (ret)
  6886. goto free_dev;
  6887. dev_set_drvdata(pmu->dev, pmu);
  6888. pmu->dev->bus = &pmu_bus;
  6889. pmu->dev->release = pmu_dev_release;
  6890. ret = device_add(pmu->dev);
  6891. if (ret)
  6892. goto free_dev;
  6893. /* For PMUs with address filters, throw in an extra attribute: */
  6894. if (pmu->nr_addr_filters)
  6895. ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
  6896. if (ret)
  6897. goto del_dev;
  6898. out:
  6899. return ret;
  6900. del_dev:
  6901. device_del(pmu->dev);
  6902. free_dev:
  6903. put_device(pmu->dev);
  6904. goto out;
  6905. }
  6906. static struct lock_class_key cpuctx_mutex;
  6907. static struct lock_class_key cpuctx_lock;
  6908. int perf_pmu_register(struct pmu *pmu, const char *name, int type)
  6909. {
  6910. int cpu, ret;
  6911. mutex_lock(&pmus_lock);
  6912. ret = -ENOMEM;
  6913. pmu->pmu_disable_count = alloc_percpu(int);
  6914. if (!pmu->pmu_disable_count)
  6915. goto unlock;
  6916. pmu->type = -1;
  6917. if (!name)
  6918. goto skip_type;
  6919. pmu->name = name;
  6920. if (type < 0) {
  6921. type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
  6922. if (type < 0) {
  6923. ret = type;
  6924. goto free_pdc;
  6925. }
  6926. }
  6927. pmu->type = type;
  6928. if (pmu_bus_running) {
  6929. ret = pmu_dev_alloc(pmu);
  6930. if (ret)
  6931. goto free_idr;
  6932. }
  6933. skip_type:
  6934. if (pmu->task_ctx_nr == perf_hw_context) {
  6935. static int hw_context_taken = 0;
  6936. /*
  6937. * Other than systems with heterogeneous CPUs, it never makes
  6938. * sense for two PMUs to share perf_hw_context. PMUs which are
  6939. * uncore must use perf_invalid_context.
  6940. */
  6941. if (WARN_ON_ONCE(hw_context_taken &&
  6942. !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
  6943. pmu->task_ctx_nr = perf_invalid_context;
  6944. hw_context_taken = 1;
  6945. }
  6946. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  6947. if (pmu->pmu_cpu_context)
  6948. goto got_cpu_context;
  6949. ret = -ENOMEM;
  6950. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  6951. if (!pmu->pmu_cpu_context)
  6952. goto free_dev;
  6953. for_each_possible_cpu(cpu) {
  6954. struct perf_cpu_context *cpuctx;
  6955. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  6956. __perf_event_init_context(&cpuctx->ctx);
  6957. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  6958. lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
  6959. cpuctx->ctx.pmu = pmu;
  6960. __perf_mux_hrtimer_init(cpuctx, cpu);
  6961. cpuctx->unique_pmu = pmu;
  6962. }
  6963. got_cpu_context:
  6964. if (!pmu->start_txn) {
  6965. if (pmu->pmu_enable) {
  6966. /*
  6967. * If we have pmu_enable/pmu_disable calls, install
  6968. * transaction stubs that use that to try and batch
  6969. * hardware accesses.
  6970. */
  6971. pmu->start_txn = perf_pmu_start_txn;
  6972. pmu->commit_txn = perf_pmu_commit_txn;
  6973. pmu->cancel_txn = perf_pmu_cancel_txn;
  6974. } else {
  6975. pmu->start_txn = perf_pmu_nop_txn;
  6976. pmu->commit_txn = perf_pmu_nop_int;
  6977. pmu->cancel_txn = perf_pmu_nop_void;
  6978. }
  6979. }
  6980. if (!pmu->pmu_enable) {
  6981. pmu->pmu_enable = perf_pmu_nop_void;
  6982. pmu->pmu_disable = perf_pmu_nop_void;
  6983. }
  6984. if (!pmu->event_idx)
  6985. pmu->event_idx = perf_event_idx_default;
  6986. list_add_rcu(&pmu->entry, &pmus);
  6987. atomic_set(&pmu->exclusive_cnt, 0);
  6988. ret = 0;
  6989. unlock:
  6990. mutex_unlock(&pmus_lock);
  6991. return ret;
  6992. free_dev:
  6993. device_del(pmu->dev);
  6994. put_device(pmu->dev);
  6995. free_idr:
  6996. if (pmu->type >= PERF_TYPE_MAX)
  6997. idr_remove(&pmu_idr, pmu->type);
  6998. free_pdc:
  6999. free_percpu(pmu->pmu_disable_count);
  7000. goto unlock;
  7001. }
  7002. EXPORT_SYMBOL_GPL(perf_pmu_register);
  7003. void perf_pmu_unregister(struct pmu *pmu)
  7004. {
  7005. mutex_lock(&pmus_lock);
  7006. list_del_rcu(&pmu->entry);
  7007. mutex_unlock(&pmus_lock);
  7008. /*
  7009. * We dereference the pmu list under both SRCU and regular RCU, so
  7010. * synchronize against both of those.
  7011. */
  7012. synchronize_srcu(&pmus_srcu);
  7013. synchronize_rcu();
  7014. free_percpu(pmu->pmu_disable_count);
  7015. if (pmu->type >= PERF_TYPE_MAX)
  7016. idr_remove(&pmu_idr, pmu->type);
  7017. if (pmu->nr_addr_filters)
  7018. device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
  7019. device_del(pmu->dev);
  7020. put_device(pmu->dev);
  7021. free_pmu_context(pmu);
  7022. }
  7023. EXPORT_SYMBOL_GPL(perf_pmu_unregister);
  7024. static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
  7025. {
  7026. struct perf_event_context *ctx = NULL;
  7027. int ret;
  7028. if (!try_module_get(pmu->module))
  7029. return -ENODEV;
  7030. if (event->group_leader != event) {
  7031. /*
  7032. * This ctx->mutex can nest when we're called through
  7033. * inheritance. See the perf_event_ctx_lock_nested() comment.
  7034. */
  7035. ctx = perf_event_ctx_lock_nested(event->group_leader,
  7036. SINGLE_DEPTH_NESTING);
  7037. BUG_ON(!ctx);
  7038. }
  7039. event->pmu = pmu;
  7040. ret = pmu->event_init(event);
  7041. if (ctx)
  7042. perf_event_ctx_unlock(event->group_leader, ctx);
  7043. if (ret)
  7044. module_put(pmu->module);
  7045. return ret;
  7046. }
  7047. static struct pmu *perf_init_event(struct perf_event *event)
  7048. {
  7049. struct pmu *pmu = NULL;
  7050. int idx;
  7051. int ret;
  7052. idx = srcu_read_lock(&pmus_srcu);
  7053. rcu_read_lock();
  7054. pmu = idr_find(&pmu_idr, event->attr.type);
  7055. rcu_read_unlock();
  7056. if (pmu) {
  7057. ret = perf_try_init_event(pmu, event);
  7058. if (ret)
  7059. pmu = ERR_PTR(ret);
  7060. goto unlock;
  7061. }
  7062. list_for_each_entry_rcu(pmu, &pmus, entry) {
  7063. ret = perf_try_init_event(pmu, event);
  7064. if (!ret)
  7065. goto unlock;
  7066. if (ret != -ENOENT) {
  7067. pmu = ERR_PTR(ret);
  7068. goto unlock;
  7069. }
  7070. }
  7071. pmu = ERR_PTR(-ENOENT);
  7072. unlock:
  7073. srcu_read_unlock(&pmus_srcu, idx);
  7074. return pmu;
  7075. }
  7076. static void account_event_cpu(struct perf_event *event, int cpu)
  7077. {
  7078. if (event->parent)
  7079. return;
  7080. if (is_cgroup_event(event))
  7081. atomic_inc(&per_cpu(perf_cgroup_events, cpu));
  7082. }
  7083. /* Freq events need the tick to stay alive (see perf_event_task_tick). */
  7084. static void account_freq_event_nohz(void)
  7085. {
  7086. #ifdef CONFIG_NO_HZ_FULL
  7087. /* Lock so we don't race with concurrent unaccount */
  7088. spin_lock(&nr_freq_lock);
  7089. if (atomic_inc_return(&nr_freq_events) == 1)
  7090. tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
  7091. spin_unlock(&nr_freq_lock);
  7092. #endif
  7093. }
  7094. static void account_freq_event(void)
  7095. {
  7096. if (tick_nohz_full_enabled())
  7097. account_freq_event_nohz();
  7098. else
  7099. atomic_inc(&nr_freq_events);
  7100. }
  7101. static void account_event(struct perf_event *event)
  7102. {
  7103. bool inc = false;
  7104. if (event->parent)
  7105. return;
  7106. if (event->attach_state & PERF_ATTACH_TASK)
  7107. inc = true;
  7108. if (event->attr.mmap || event->attr.mmap_data)
  7109. atomic_inc(&nr_mmap_events);
  7110. if (event->attr.comm)
  7111. atomic_inc(&nr_comm_events);
  7112. if (event->attr.task)
  7113. atomic_inc(&nr_task_events);
  7114. if (event->attr.freq)
  7115. account_freq_event();
  7116. if (event->attr.context_switch) {
  7117. atomic_inc(&nr_switch_events);
  7118. inc = true;
  7119. }
  7120. if (has_branch_stack(event))
  7121. inc = true;
  7122. if (is_cgroup_event(event))
  7123. inc = true;
  7124. if (inc) {
  7125. if (atomic_inc_not_zero(&perf_sched_count))
  7126. goto enabled;
  7127. mutex_lock(&perf_sched_mutex);
  7128. if (!atomic_read(&perf_sched_count)) {
  7129. static_branch_enable(&perf_sched_events);
  7130. /*
  7131. * Guarantee that all CPUs observe they key change and
  7132. * call the perf scheduling hooks before proceeding to
  7133. * install events that need them.
  7134. */
  7135. synchronize_sched();
  7136. }
  7137. /*
  7138. * Now that we have waited for the sync_sched(), allow further
  7139. * increments to by-pass the mutex.
  7140. */
  7141. atomic_inc(&perf_sched_count);
  7142. mutex_unlock(&perf_sched_mutex);
  7143. }
  7144. enabled:
  7145. account_event_cpu(event, event->cpu);
  7146. }
  7147. /*
  7148. * Allocate and initialize a event structure
  7149. */
  7150. static struct perf_event *
  7151. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  7152. struct task_struct *task,
  7153. struct perf_event *group_leader,
  7154. struct perf_event *parent_event,
  7155. perf_overflow_handler_t overflow_handler,
  7156. void *context, int cgroup_fd)
  7157. {
  7158. struct pmu *pmu;
  7159. struct perf_event *event;
  7160. struct hw_perf_event *hwc;
  7161. long err = -EINVAL;
  7162. if ((unsigned)cpu >= nr_cpu_ids) {
  7163. if (!task || cpu != -1)
  7164. return ERR_PTR(-EINVAL);
  7165. }
  7166. event = kzalloc(sizeof(*event), GFP_KERNEL);
  7167. if (!event)
  7168. return ERR_PTR(-ENOMEM);
  7169. /*
  7170. * Single events are their own group leaders, with an
  7171. * empty sibling list:
  7172. */
  7173. if (!group_leader)
  7174. group_leader = event;
  7175. mutex_init(&event->child_mutex);
  7176. INIT_LIST_HEAD(&event->child_list);
  7177. INIT_LIST_HEAD(&event->group_entry);
  7178. INIT_LIST_HEAD(&event->event_entry);
  7179. INIT_LIST_HEAD(&event->sibling_list);
  7180. INIT_LIST_HEAD(&event->rb_entry);
  7181. INIT_LIST_HEAD(&event->active_entry);
  7182. INIT_LIST_HEAD(&event->addr_filters.list);
  7183. INIT_HLIST_NODE(&event->hlist_entry);
  7184. init_waitqueue_head(&event->waitq);
  7185. init_irq_work(&event->pending, perf_pending_event);
  7186. mutex_init(&event->mmap_mutex);
  7187. raw_spin_lock_init(&event->addr_filters.lock);
  7188. atomic_long_set(&event->refcount, 1);
  7189. event->cpu = cpu;
  7190. event->attr = *attr;
  7191. event->group_leader = group_leader;
  7192. event->pmu = NULL;
  7193. event->oncpu = -1;
  7194. event->parent = parent_event;
  7195. event->ns = get_pid_ns(task_active_pid_ns(current));
  7196. event->id = atomic64_inc_return(&perf_event_id);
  7197. event->state = PERF_EVENT_STATE_INACTIVE;
  7198. if (task) {
  7199. event->attach_state = PERF_ATTACH_TASK;
  7200. /*
  7201. * XXX pmu::event_init needs to know what task to account to
  7202. * and we cannot use the ctx information because we need the
  7203. * pmu before we get a ctx.
  7204. */
  7205. event->hw.target = task;
  7206. }
  7207. event->clock = &local_clock;
  7208. if (parent_event)
  7209. event->clock = parent_event->clock;
  7210. if (!overflow_handler && parent_event) {
  7211. overflow_handler = parent_event->overflow_handler;
  7212. context = parent_event->overflow_handler_context;
  7213. }
  7214. if (overflow_handler) {
  7215. event->overflow_handler = overflow_handler;
  7216. event->overflow_handler_context = context;
  7217. } else if (is_write_backward(event)){
  7218. event->overflow_handler = perf_event_output_backward;
  7219. event->overflow_handler_context = NULL;
  7220. } else {
  7221. event->overflow_handler = perf_event_output_forward;
  7222. event->overflow_handler_context = NULL;
  7223. }
  7224. perf_event__state_init(event);
  7225. pmu = NULL;
  7226. hwc = &event->hw;
  7227. hwc->sample_period = attr->sample_period;
  7228. if (attr->freq && attr->sample_freq)
  7229. hwc->sample_period = 1;
  7230. hwc->last_period = hwc->sample_period;
  7231. local64_set(&hwc->period_left, hwc->sample_period);
  7232. /*
  7233. * we currently do not support PERF_FORMAT_GROUP on inherited events
  7234. */
  7235. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  7236. goto err_ns;
  7237. if (!has_branch_stack(event))
  7238. event->attr.branch_sample_type = 0;
  7239. if (cgroup_fd != -1) {
  7240. err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
  7241. if (err)
  7242. goto err_ns;
  7243. }
  7244. pmu = perf_init_event(event);
  7245. if (!pmu)
  7246. goto err_ns;
  7247. else if (IS_ERR(pmu)) {
  7248. err = PTR_ERR(pmu);
  7249. goto err_ns;
  7250. }
  7251. err = exclusive_event_init(event);
  7252. if (err)
  7253. goto err_pmu;
  7254. if (has_addr_filter(event)) {
  7255. event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
  7256. sizeof(unsigned long),
  7257. GFP_KERNEL);
  7258. if (!event->addr_filters_offs)
  7259. goto err_per_task;
  7260. /* force hw sync on the address filters */
  7261. event->addr_filters_gen = 1;
  7262. }
  7263. if (!event->parent) {
  7264. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  7265. err = get_callchain_buffers();
  7266. if (err)
  7267. goto err_addr_filters;
  7268. }
  7269. }
  7270. /* symmetric to unaccount_event() in _free_event() */
  7271. account_event(event);
  7272. return event;
  7273. err_addr_filters:
  7274. kfree(event->addr_filters_offs);
  7275. err_per_task:
  7276. exclusive_event_destroy(event);
  7277. err_pmu:
  7278. if (event->destroy)
  7279. event->destroy(event);
  7280. module_put(pmu->module);
  7281. err_ns:
  7282. if (is_cgroup_event(event))
  7283. perf_detach_cgroup(event);
  7284. if (event->ns)
  7285. put_pid_ns(event->ns);
  7286. kfree(event);
  7287. return ERR_PTR(err);
  7288. }
  7289. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  7290. struct perf_event_attr *attr)
  7291. {
  7292. u32 size;
  7293. int ret;
  7294. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  7295. return -EFAULT;
  7296. /*
  7297. * zero the full structure, so that a short copy will be nice.
  7298. */
  7299. memset(attr, 0, sizeof(*attr));
  7300. ret = get_user(size, &uattr->size);
  7301. if (ret)
  7302. return ret;
  7303. if (size > PAGE_SIZE) /* silly large */
  7304. goto err_size;
  7305. if (!size) /* abi compat */
  7306. size = PERF_ATTR_SIZE_VER0;
  7307. if (size < PERF_ATTR_SIZE_VER0)
  7308. goto err_size;
  7309. /*
  7310. * If we're handed a bigger struct than we know of,
  7311. * ensure all the unknown bits are 0 - i.e. new
  7312. * user-space does not rely on any kernel feature
  7313. * extensions we dont know about yet.
  7314. */
  7315. if (size > sizeof(*attr)) {
  7316. unsigned char __user *addr;
  7317. unsigned char __user *end;
  7318. unsigned char val;
  7319. addr = (void __user *)uattr + sizeof(*attr);
  7320. end = (void __user *)uattr + size;
  7321. for (; addr < end; addr++) {
  7322. ret = get_user(val, addr);
  7323. if (ret)
  7324. return ret;
  7325. if (val)
  7326. goto err_size;
  7327. }
  7328. size = sizeof(*attr);
  7329. }
  7330. ret = copy_from_user(attr, uattr, size);
  7331. if (ret)
  7332. return -EFAULT;
  7333. if (attr->__reserved_1)
  7334. return -EINVAL;
  7335. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  7336. return -EINVAL;
  7337. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  7338. return -EINVAL;
  7339. if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
  7340. u64 mask = attr->branch_sample_type;
  7341. /* only using defined bits */
  7342. if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
  7343. return -EINVAL;
  7344. /* at least one branch bit must be set */
  7345. if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
  7346. return -EINVAL;
  7347. /* propagate priv level, when not set for branch */
  7348. if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
  7349. /* exclude_kernel checked on syscall entry */
  7350. if (!attr->exclude_kernel)
  7351. mask |= PERF_SAMPLE_BRANCH_KERNEL;
  7352. if (!attr->exclude_user)
  7353. mask |= PERF_SAMPLE_BRANCH_USER;
  7354. if (!attr->exclude_hv)
  7355. mask |= PERF_SAMPLE_BRANCH_HV;
  7356. /*
  7357. * adjust user setting (for HW filter setup)
  7358. */
  7359. attr->branch_sample_type = mask;
  7360. }
  7361. /* privileged levels capture (kernel, hv): check permissions */
  7362. if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
  7363. && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  7364. return -EACCES;
  7365. }
  7366. if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
  7367. ret = perf_reg_validate(attr->sample_regs_user);
  7368. if (ret)
  7369. return ret;
  7370. }
  7371. if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
  7372. if (!arch_perf_have_user_stack_dump())
  7373. return -ENOSYS;
  7374. /*
  7375. * We have __u32 type for the size, but so far
  7376. * we can only use __u16 as maximum due to the
  7377. * __u16 sample size limit.
  7378. */
  7379. if (attr->sample_stack_user >= USHRT_MAX)
  7380. ret = -EINVAL;
  7381. else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
  7382. ret = -EINVAL;
  7383. }
  7384. if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
  7385. ret = perf_reg_validate(attr->sample_regs_intr);
  7386. out:
  7387. return ret;
  7388. err_size:
  7389. put_user(sizeof(*attr), &uattr->size);
  7390. ret = -E2BIG;
  7391. goto out;
  7392. }
  7393. static int
  7394. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  7395. {
  7396. struct ring_buffer *rb = NULL;
  7397. int ret = -EINVAL;
  7398. if (!output_event)
  7399. goto set;
  7400. /* don't allow circular references */
  7401. if (event == output_event)
  7402. goto out;
  7403. /*
  7404. * Don't allow cross-cpu buffers
  7405. */
  7406. if (output_event->cpu != event->cpu)
  7407. goto out;
  7408. /*
  7409. * If its not a per-cpu rb, it must be the same task.
  7410. */
  7411. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  7412. goto out;
  7413. /*
  7414. * Mixing clocks in the same buffer is trouble you don't need.
  7415. */
  7416. if (output_event->clock != event->clock)
  7417. goto out;
  7418. /*
  7419. * Either writing ring buffer from beginning or from end.
  7420. * Mixing is not allowed.
  7421. */
  7422. if (is_write_backward(output_event) != is_write_backward(event))
  7423. goto out;
  7424. /*
  7425. * If both events generate aux data, they must be on the same PMU
  7426. */
  7427. if (has_aux(event) && has_aux(output_event) &&
  7428. event->pmu != output_event->pmu)
  7429. goto out;
  7430. set:
  7431. mutex_lock(&event->mmap_mutex);
  7432. /* Can't redirect output if we've got an active mmap() */
  7433. if (atomic_read(&event->mmap_count))
  7434. goto unlock;
  7435. if (output_event) {
  7436. /* get the rb we want to redirect to */
  7437. rb = ring_buffer_get(output_event);
  7438. if (!rb)
  7439. goto unlock;
  7440. }
  7441. ring_buffer_attach(event, rb);
  7442. ret = 0;
  7443. unlock:
  7444. mutex_unlock(&event->mmap_mutex);
  7445. out:
  7446. return ret;
  7447. }
  7448. static void mutex_lock_double(struct mutex *a, struct mutex *b)
  7449. {
  7450. if (b < a)
  7451. swap(a, b);
  7452. mutex_lock(a);
  7453. mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
  7454. }
  7455. static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
  7456. {
  7457. bool nmi_safe = false;
  7458. switch (clk_id) {
  7459. case CLOCK_MONOTONIC:
  7460. event->clock = &ktime_get_mono_fast_ns;
  7461. nmi_safe = true;
  7462. break;
  7463. case CLOCK_MONOTONIC_RAW:
  7464. event->clock = &ktime_get_raw_fast_ns;
  7465. nmi_safe = true;
  7466. break;
  7467. case CLOCK_REALTIME:
  7468. event->clock = &ktime_get_real_ns;
  7469. break;
  7470. case CLOCK_BOOTTIME:
  7471. event->clock = &ktime_get_boot_ns;
  7472. break;
  7473. case CLOCK_TAI:
  7474. event->clock = &ktime_get_tai_ns;
  7475. break;
  7476. default:
  7477. return -EINVAL;
  7478. }
  7479. if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
  7480. return -EINVAL;
  7481. return 0;
  7482. }
  7483. /**
  7484. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  7485. *
  7486. * @attr_uptr: event_id type attributes for monitoring/sampling
  7487. * @pid: target pid
  7488. * @cpu: target cpu
  7489. * @group_fd: group leader event fd
  7490. */
  7491. SYSCALL_DEFINE5(perf_event_open,
  7492. struct perf_event_attr __user *, attr_uptr,
  7493. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  7494. {
  7495. struct perf_event *group_leader = NULL, *output_event = NULL;
  7496. struct perf_event *event, *sibling;
  7497. struct perf_event_attr attr;
  7498. struct perf_event_context *ctx, *uninitialized_var(gctx);
  7499. struct file *event_file = NULL;
  7500. struct fd group = {NULL, 0};
  7501. struct task_struct *task = NULL;
  7502. struct pmu *pmu;
  7503. int event_fd;
  7504. int move_group = 0;
  7505. int err;
  7506. int f_flags = O_RDWR;
  7507. int cgroup_fd = -1;
  7508. /* for future expandability... */
  7509. if (flags & ~PERF_FLAG_ALL)
  7510. return -EINVAL;
  7511. err = perf_copy_attr(attr_uptr, &attr);
  7512. if (err)
  7513. return err;
  7514. if (!attr.exclude_kernel) {
  7515. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  7516. return -EACCES;
  7517. }
  7518. if (attr.freq) {
  7519. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  7520. return -EINVAL;
  7521. } else {
  7522. if (attr.sample_period & (1ULL << 63))
  7523. return -EINVAL;
  7524. }
  7525. /*
  7526. * In cgroup mode, the pid argument is used to pass the fd
  7527. * opened to the cgroup directory in cgroupfs. The cpu argument
  7528. * designates the cpu on which to monitor threads from that
  7529. * cgroup.
  7530. */
  7531. if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
  7532. return -EINVAL;
  7533. if (flags & PERF_FLAG_FD_CLOEXEC)
  7534. f_flags |= O_CLOEXEC;
  7535. event_fd = get_unused_fd_flags(f_flags);
  7536. if (event_fd < 0)
  7537. return event_fd;
  7538. if (group_fd != -1) {
  7539. err = perf_fget_light(group_fd, &group);
  7540. if (err)
  7541. goto err_fd;
  7542. group_leader = group.file->private_data;
  7543. if (flags & PERF_FLAG_FD_OUTPUT)
  7544. output_event = group_leader;
  7545. if (flags & PERF_FLAG_FD_NO_GROUP)
  7546. group_leader = NULL;
  7547. }
  7548. if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
  7549. task = find_lively_task_by_vpid(pid);
  7550. if (IS_ERR(task)) {
  7551. err = PTR_ERR(task);
  7552. goto err_group_fd;
  7553. }
  7554. }
  7555. if (task && group_leader &&
  7556. group_leader->attr.inherit != attr.inherit) {
  7557. err = -EINVAL;
  7558. goto err_task;
  7559. }
  7560. get_online_cpus();
  7561. if (task) {
  7562. err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
  7563. if (err)
  7564. goto err_cpus;
  7565. /*
  7566. * Reuse ptrace permission checks for now.
  7567. *
  7568. * We must hold cred_guard_mutex across this and any potential
  7569. * perf_install_in_context() call for this new event to
  7570. * serialize against exec() altering our credentials (and the
  7571. * perf_event_exit_task() that could imply).
  7572. */
  7573. err = -EACCES;
  7574. if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
  7575. goto err_cred;
  7576. }
  7577. if (flags & PERF_FLAG_PID_CGROUP)
  7578. cgroup_fd = pid;
  7579. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
  7580. NULL, NULL, cgroup_fd);
  7581. if (IS_ERR(event)) {
  7582. err = PTR_ERR(event);
  7583. goto err_cred;
  7584. }
  7585. if (is_sampling_event(event)) {
  7586. if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
  7587. err = -ENOTSUPP;
  7588. goto err_alloc;
  7589. }
  7590. }
  7591. /*
  7592. * Special case software events and allow them to be part of
  7593. * any hardware group.
  7594. */
  7595. pmu = event->pmu;
  7596. if (attr.use_clockid) {
  7597. err = perf_event_set_clock(event, attr.clockid);
  7598. if (err)
  7599. goto err_alloc;
  7600. }
  7601. if (group_leader &&
  7602. (is_software_event(event) != is_software_event(group_leader))) {
  7603. if (is_software_event(event)) {
  7604. /*
  7605. * If event and group_leader are not both a software
  7606. * event, and event is, then group leader is not.
  7607. *
  7608. * Allow the addition of software events to !software
  7609. * groups, this is safe because software events never
  7610. * fail to schedule.
  7611. */
  7612. pmu = group_leader->pmu;
  7613. } else if (is_software_event(group_leader) &&
  7614. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  7615. /*
  7616. * In case the group is a pure software group, and we
  7617. * try to add a hardware event, move the whole group to
  7618. * the hardware context.
  7619. */
  7620. move_group = 1;
  7621. }
  7622. }
  7623. /*
  7624. * Get the target context (task or percpu):
  7625. */
  7626. ctx = find_get_context(pmu, task, event);
  7627. if (IS_ERR(ctx)) {
  7628. err = PTR_ERR(ctx);
  7629. goto err_alloc;
  7630. }
  7631. if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
  7632. err = -EBUSY;
  7633. goto err_context;
  7634. }
  7635. /*
  7636. * Look up the group leader (we will attach this event to it):
  7637. */
  7638. if (group_leader) {
  7639. err = -EINVAL;
  7640. /*
  7641. * Do not allow a recursive hierarchy (this new sibling
  7642. * becoming part of another group-sibling):
  7643. */
  7644. if (group_leader->group_leader != group_leader)
  7645. goto err_context;
  7646. /* All events in a group should have the same clock */
  7647. if (group_leader->clock != event->clock)
  7648. goto err_context;
  7649. /*
  7650. * Do not allow to attach to a group in a different
  7651. * task or CPU context:
  7652. */
  7653. if (move_group) {
  7654. /*
  7655. * Make sure we're both on the same task, or both
  7656. * per-cpu events.
  7657. */
  7658. if (group_leader->ctx->task != ctx->task)
  7659. goto err_context;
  7660. /*
  7661. * Make sure we're both events for the same CPU;
  7662. * grouping events for different CPUs is broken; since
  7663. * you can never concurrently schedule them anyhow.
  7664. */
  7665. if (group_leader->cpu != event->cpu)
  7666. goto err_context;
  7667. } else {
  7668. if (group_leader->ctx != ctx)
  7669. goto err_context;
  7670. }
  7671. /*
  7672. * Only a group leader can be exclusive or pinned
  7673. */
  7674. if (attr.exclusive || attr.pinned)
  7675. goto err_context;
  7676. }
  7677. if (output_event) {
  7678. err = perf_event_set_output(event, output_event);
  7679. if (err)
  7680. goto err_context;
  7681. }
  7682. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
  7683. f_flags);
  7684. if (IS_ERR(event_file)) {
  7685. err = PTR_ERR(event_file);
  7686. event_file = NULL;
  7687. goto err_context;
  7688. }
  7689. if (move_group) {
  7690. gctx = group_leader->ctx;
  7691. mutex_lock_double(&gctx->mutex, &ctx->mutex);
  7692. if (gctx->task == TASK_TOMBSTONE) {
  7693. err = -ESRCH;
  7694. goto err_locked;
  7695. }
  7696. } else {
  7697. mutex_lock(&ctx->mutex);
  7698. }
  7699. if (ctx->task == TASK_TOMBSTONE) {
  7700. err = -ESRCH;
  7701. goto err_locked;
  7702. }
  7703. if (!perf_event_validate_size(event)) {
  7704. err = -E2BIG;
  7705. goto err_locked;
  7706. }
  7707. /*
  7708. * Must be under the same ctx::mutex as perf_install_in_context(),
  7709. * because we need to serialize with concurrent event creation.
  7710. */
  7711. if (!exclusive_event_installable(event, ctx)) {
  7712. /* exclusive and group stuff are assumed mutually exclusive */
  7713. WARN_ON_ONCE(move_group);
  7714. err = -EBUSY;
  7715. goto err_locked;
  7716. }
  7717. WARN_ON_ONCE(ctx->parent_ctx);
  7718. /*
  7719. * This is the point on no return; we cannot fail hereafter. This is
  7720. * where we start modifying current state.
  7721. */
  7722. if (move_group) {
  7723. /*
  7724. * See perf_event_ctx_lock() for comments on the details
  7725. * of swizzling perf_event::ctx.
  7726. */
  7727. perf_remove_from_context(group_leader, 0);
  7728. list_for_each_entry(sibling, &group_leader->sibling_list,
  7729. group_entry) {
  7730. perf_remove_from_context(sibling, 0);
  7731. put_ctx(gctx);
  7732. }
  7733. /*
  7734. * Wait for everybody to stop referencing the events through
  7735. * the old lists, before installing it on new lists.
  7736. */
  7737. synchronize_rcu();
  7738. /*
  7739. * Install the group siblings before the group leader.
  7740. *
  7741. * Because a group leader will try and install the entire group
  7742. * (through the sibling list, which is still in-tact), we can
  7743. * end up with siblings installed in the wrong context.
  7744. *
  7745. * By installing siblings first we NO-OP because they're not
  7746. * reachable through the group lists.
  7747. */
  7748. list_for_each_entry(sibling, &group_leader->sibling_list,
  7749. group_entry) {
  7750. perf_event__state_init(sibling);
  7751. perf_install_in_context(ctx, sibling, sibling->cpu);
  7752. get_ctx(ctx);
  7753. }
  7754. /*
  7755. * Removing from the context ends up with disabled
  7756. * event. What we want here is event in the initial
  7757. * startup state, ready to be add into new context.
  7758. */
  7759. perf_event__state_init(group_leader);
  7760. perf_install_in_context(ctx, group_leader, group_leader->cpu);
  7761. get_ctx(ctx);
  7762. /*
  7763. * Now that all events are installed in @ctx, nothing
  7764. * references @gctx anymore, so drop the last reference we have
  7765. * on it.
  7766. */
  7767. put_ctx(gctx);
  7768. }
  7769. /*
  7770. * Precalculate sample_data sizes; do while holding ctx::mutex such
  7771. * that we're serialized against further additions and before
  7772. * perf_install_in_context() which is the point the event is active and
  7773. * can use these values.
  7774. */
  7775. perf_event__header_size(event);
  7776. perf_event__id_header_size(event);
  7777. event->owner = current;
  7778. perf_install_in_context(ctx, event, event->cpu);
  7779. perf_unpin_context(ctx);
  7780. if (move_group)
  7781. mutex_unlock(&gctx->mutex);
  7782. mutex_unlock(&ctx->mutex);
  7783. if (task) {
  7784. mutex_unlock(&task->signal->cred_guard_mutex);
  7785. put_task_struct(task);
  7786. }
  7787. put_online_cpus();
  7788. mutex_lock(&current->perf_event_mutex);
  7789. list_add_tail(&event->owner_entry, &current->perf_event_list);
  7790. mutex_unlock(&current->perf_event_mutex);
  7791. /*
  7792. * Drop the reference on the group_event after placing the
  7793. * new event on the sibling_list. This ensures destruction
  7794. * of the group leader will find the pointer to itself in
  7795. * perf_group_detach().
  7796. */
  7797. fdput(group);
  7798. fd_install(event_fd, event_file);
  7799. return event_fd;
  7800. err_locked:
  7801. if (move_group)
  7802. mutex_unlock(&gctx->mutex);
  7803. mutex_unlock(&ctx->mutex);
  7804. /* err_file: */
  7805. fput(event_file);
  7806. err_context:
  7807. perf_unpin_context(ctx);
  7808. put_ctx(ctx);
  7809. err_alloc:
  7810. /*
  7811. * If event_file is set, the fput() above will have called ->release()
  7812. * and that will take care of freeing the event.
  7813. */
  7814. if (!event_file)
  7815. free_event(event);
  7816. err_cred:
  7817. if (task)
  7818. mutex_unlock(&task->signal->cred_guard_mutex);
  7819. err_cpus:
  7820. put_online_cpus();
  7821. err_task:
  7822. if (task)
  7823. put_task_struct(task);
  7824. err_group_fd:
  7825. fdput(group);
  7826. err_fd:
  7827. put_unused_fd(event_fd);
  7828. return err;
  7829. }
  7830. /**
  7831. * perf_event_create_kernel_counter
  7832. *
  7833. * @attr: attributes of the counter to create
  7834. * @cpu: cpu in which the counter is bound
  7835. * @task: task to profile (NULL for percpu)
  7836. */
  7837. struct perf_event *
  7838. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  7839. struct task_struct *task,
  7840. perf_overflow_handler_t overflow_handler,
  7841. void *context)
  7842. {
  7843. struct perf_event_context *ctx;
  7844. struct perf_event *event;
  7845. int err;
  7846. /*
  7847. * Get the target context (task or percpu):
  7848. */
  7849. event = perf_event_alloc(attr, cpu, task, NULL, NULL,
  7850. overflow_handler, context, -1);
  7851. if (IS_ERR(event)) {
  7852. err = PTR_ERR(event);
  7853. goto err;
  7854. }
  7855. /* Mark owner so we could distinguish it from user events. */
  7856. event->owner = TASK_TOMBSTONE;
  7857. ctx = find_get_context(event->pmu, task, event);
  7858. if (IS_ERR(ctx)) {
  7859. err = PTR_ERR(ctx);
  7860. goto err_free;
  7861. }
  7862. WARN_ON_ONCE(ctx->parent_ctx);
  7863. mutex_lock(&ctx->mutex);
  7864. if (ctx->task == TASK_TOMBSTONE) {
  7865. err = -ESRCH;
  7866. goto err_unlock;
  7867. }
  7868. if (!exclusive_event_installable(event, ctx)) {
  7869. err = -EBUSY;
  7870. goto err_unlock;
  7871. }
  7872. perf_install_in_context(ctx, event, cpu);
  7873. perf_unpin_context(ctx);
  7874. mutex_unlock(&ctx->mutex);
  7875. return event;
  7876. err_unlock:
  7877. mutex_unlock(&ctx->mutex);
  7878. perf_unpin_context(ctx);
  7879. put_ctx(ctx);
  7880. err_free:
  7881. free_event(event);
  7882. err:
  7883. return ERR_PTR(err);
  7884. }
  7885. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  7886. void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
  7887. {
  7888. struct perf_event_context *src_ctx;
  7889. struct perf_event_context *dst_ctx;
  7890. struct perf_event *event, *tmp;
  7891. LIST_HEAD(events);
  7892. src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
  7893. dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
  7894. /*
  7895. * See perf_event_ctx_lock() for comments on the details
  7896. * of swizzling perf_event::ctx.
  7897. */
  7898. mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
  7899. list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
  7900. event_entry) {
  7901. perf_remove_from_context(event, 0);
  7902. unaccount_event_cpu(event, src_cpu);
  7903. put_ctx(src_ctx);
  7904. list_add(&event->migrate_entry, &events);
  7905. }
  7906. /*
  7907. * Wait for the events to quiesce before re-instating them.
  7908. */
  7909. synchronize_rcu();
  7910. /*
  7911. * Re-instate events in 2 passes.
  7912. *
  7913. * Skip over group leaders and only install siblings on this first
  7914. * pass, siblings will not get enabled without a leader, however a
  7915. * leader will enable its siblings, even if those are still on the old
  7916. * context.
  7917. */
  7918. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  7919. if (event->group_leader == event)
  7920. continue;
  7921. list_del(&event->migrate_entry);
  7922. if (event->state >= PERF_EVENT_STATE_OFF)
  7923. event->state = PERF_EVENT_STATE_INACTIVE;
  7924. account_event_cpu(event, dst_cpu);
  7925. perf_install_in_context(dst_ctx, event, dst_cpu);
  7926. get_ctx(dst_ctx);
  7927. }
  7928. /*
  7929. * Once all the siblings are setup properly, install the group leaders
  7930. * to make it go.
  7931. */
  7932. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  7933. list_del(&event->migrate_entry);
  7934. if (event->state >= PERF_EVENT_STATE_OFF)
  7935. event->state = PERF_EVENT_STATE_INACTIVE;
  7936. account_event_cpu(event, dst_cpu);
  7937. perf_install_in_context(dst_ctx, event, dst_cpu);
  7938. get_ctx(dst_ctx);
  7939. }
  7940. mutex_unlock(&dst_ctx->mutex);
  7941. mutex_unlock(&src_ctx->mutex);
  7942. }
  7943. EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
  7944. static void sync_child_event(struct perf_event *child_event,
  7945. struct task_struct *child)
  7946. {
  7947. struct perf_event *parent_event = child_event->parent;
  7948. u64 child_val;
  7949. if (child_event->attr.inherit_stat)
  7950. perf_event_read_event(child_event, child);
  7951. child_val = perf_event_count(child_event);
  7952. /*
  7953. * Add back the child's count to the parent's count:
  7954. */
  7955. atomic64_add(child_val, &parent_event->child_count);
  7956. atomic64_add(child_event->total_time_enabled,
  7957. &parent_event->child_total_time_enabled);
  7958. atomic64_add(child_event->total_time_running,
  7959. &parent_event->child_total_time_running);
  7960. }
  7961. static void
  7962. perf_event_exit_event(struct perf_event *child_event,
  7963. struct perf_event_context *child_ctx,
  7964. struct task_struct *child)
  7965. {
  7966. struct perf_event *parent_event = child_event->parent;
  7967. /*
  7968. * Do not destroy the 'original' grouping; because of the context
  7969. * switch optimization the original events could've ended up in a
  7970. * random child task.
  7971. *
  7972. * If we were to destroy the original group, all group related
  7973. * operations would cease to function properly after this random
  7974. * child dies.
  7975. *
  7976. * Do destroy all inherited groups, we don't care about those
  7977. * and being thorough is better.
  7978. */
  7979. raw_spin_lock_irq(&child_ctx->lock);
  7980. WARN_ON_ONCE(child_ctx->is_active);
  7981. if (parent_event)
  7982. perf_group_detach(child_event);
  7983. list_del_event(child_event, child_ctx);
  7984. child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
  7985. raw_spin_unlock_irq(&child_ctx->lock);
  7986. /*
  7987. * Parent events are governed by their filedesc, retain them.
  7988. */
  7989. if (!parent_event) {
  7990. perf_event_wakeup(child_event);
  7991. return;
  7992. }
  7993. /*
  7994. * Child events can be cleaned up.
  7995. */
  7996. sync_child_event(child_event, child);
  7997. /*
  7998. * Remove this event from the parent's list
  7999. */
  8000. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  8001. mutex_lock(&parent_event->child_mutex);
  8002. list_del_init(&child_event->child_list);
  8003. mutex_unlock(&parent_event->child_mutex);
  8004. /*
  8005. * Kick perf_poll() for is_event_hup().
  8006. */
  8007. perf_event_wakeup(parent_event);
  8008. free_event(child_event);
  8009. put_event(parent_event);
  8010. }
  8011. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  8012. {
  8013. struct perf_event_context *child_ctx, *clone_ctx = NULL;
  8014. struct perf_event *child_event, *next;
  8015. WARN_ON_ONCE(child != current);
  8016. child_ctx = perf_pin_task_context(child, ctxn);
  8017. if (!child_ctx)
  8018. return;
  8019. /*
  8020. * In order to reduce the amount of tricky in ctx tear-down, we hold
  8021. * ctx::mutex over the entire thing. This serializes against almost
  8022. * everything that wants to access the ctx.
  8023. *
  8024. * The exception is sys_perf_event_open() /
  8025. * perf_event_create_kernel_count() which does find_get_context()
  8026. * without ctx::mutex (it cannot because of the move_group double mutex
  8027. * lock thing). See the comments in perf_install_in_context().
  8028. */
  8029. mutex_lock(&child_ctx->mutex);
  8030. /*
  8031. * In a single ctx::lock section, de-schedule the events and detach the
  8032. * context from the task such that we cannot ever get it scheduled back
  8033. * in.
  8034. */
  8035. raw_spin_lock_irq(&child_ctx->lock);
  8036. task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
  8037. /*
  8038. * Now that the context is inactive, destroy the task <-> ctx relation
  8039. * and mark the context dead.
  8040. */
  8041. RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
  8042. put_ctx(child_ctx); /* cannot be last */
  8043. WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
  8044. put_task_struct(current); /* cannot be last */
  8045. clone_ctx = unclone_ctx(child_ctx);
  8046. raw_spin_unlock_irq(&child_ctx->lock);
  8047. if (clone_ctx)
  8048. put_ctx(clone_ctx);
  8049. /*
  8050. * Report the task dead after unscheduling the events so that we
  8051. * won't get any samples after PERF_RECORD_EXIT. We can however still
  8052. * get a few PERF_RECORD_READ events.
  8053. */
  8054. perf_event_task(child, child_ctx, 0);
  8055. list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
  8056. perf_event_exit_event(child_event, child_ctx, child);
  8057. mutex_unlock(&child_ctx->mutex);
  8058. put_ctx(child_ctx);
  8059. }
  8060. /*
  8061. * When a child task exits, feed back event values to parent events.
  8062. *
  8063. * Can be called with cred_guard_mutex held when called from
  8064. * install_exec_creds().
  8065. */
  8066. void perf_event_exit_task(struct task_struct *child)
  8067. {
  8068. struct perf_event *event, *tmp;
  8069. int ctxn;
  8070. mutex_lock(&child->perf_event_mutex);
  8071. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  8072. owner_entry) {
  8073. list_del_init(&event->owner_entry);
  8074. /*
  8075. * Ensure the list deletion is visible before we clear
  8076. * the owner, closes a race against perf_release() where
  8077. * we need to serialize on the owner->perf_event_mutex.
  8078. */
  8079. smp_store_release(&event->owner, NULL);
  8080. }
  8081. mutex_unlock(&child->perf_event_mutex);
  8082. for_each_task_context_nr(ctxn)
  8083. perf_event_exit_task_context(child, ctxn);
  8084. /*
  8085. * The perf_event_exit_task_context calls perf_event_task
  8086. * with child's task_ctx, which generates EXIT events for
  8087. * child contexts and sets child->perf_event_ctxp[] to NULL.
  8088. * At this point we need to send EXIT events to cpu contexts.
  8089. */
  8090. perf_event_task(child, NULL, 0);
  8091. }
  8092. static void perf_free_event(struct perf_event *event,
  8093. struct perf_event_context *ctx)
  8094. {
  8095. struct perf_event *parent = event->parent;
  8096. if (WARN_ON_ONCE(!parent))
  8097. return;
  8098. mutex_lock(&parent->child_mutex);
  8099. list_del_init(&event->child_list);
  8100. mutex_unlock(&parent->child_mutex);
  8101. put_event(parent);
  8102. raw_spin_lock_irq(&ctx->lock);
  8103. perf_group_detach(event);
  8104. list_del_event(event, ctx);
  8105. raw_spin_unlock_irq(&ctx->lock);
  8106. free_event(event);
  8107. }
  8108. /*
  8109. * Free an unexposed, unused context as created by inheritance by
  8110. * perf_event_init_task below, used by fork() in case of fail.
  8111. *
  8112. * Not all locks are strictly required, but take them anyway to be nice and
  8113. * help out with the lockdep assertions.
  8114. */
  8115. void perf_event_free_task(struct task_struct *task)
  8116. {
  8117. struct perf_event_context *ctx;
  8118. struct perf_event *event, *tmp;
  8119. int ctxn;
  8120. for_each_task_context_nr(ctxn) {
  8121. ctx = task->perf_event_ctxp[ctxn];
  8122. if (!ctx)
  8123. continue;
  8124. mutex_lock(&ctx->mutex);
  8125. again:
  8126. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  8127. group_entry)
  8128. perf_free_event(event, ctx);
  8129. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  8130. group_entry)
  8131. perf_free_event(event, ctx);
  8132. if (!list_empty(&ctx->pinned_groups) ||
  8133. !list_empty(&ctx->flexible_groups))
  8134. goto again;
  8135. mutex_unlock(&ctx->mutex);
  8136. put_ctx(ctx);
  8137. }
  8138. }
  8139. void perf_event_delayed_put(struct task_struct *task)
  8140. {
  8141. int ctxn;
  8142. for_each_task_context_nr(ctxn)
  8143. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  8144. }
  8145. struct file *perf_event_get(unsigned int fd)
  8146. {
  8147. struct file *file;
  8148. file = fget_raw(fd);
  8149. if (!file)
  8150. return ERR_PTR(-EBADF);
  8151. if (file->f_op != &perf_fops) {
  8152. fput(file);
  8153. return ERR_PTR(-EBADF);
  8154. }
  8155. return file;
  8156. }
  8157. const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
  8158. {
  8159. if (!event)
  8160. return ERR_PTR(-EINVAL);
  8161. return &event->attr;
  8162. }
  8163. /*
  8164. * inherit a event from parent task to child task:
  8165. */
  8166. static struct perf_event *
  8167. inherit_event(struct perf_event *parent_event,
  8168. struct task_struct *parent,
  8169. struct perf_event_context *parent_ctx,
  8170. struct task_struct *child,
  8171. struct perf_event *group_leader,
  8172. struct perf_event_context *child_ctx)
  8173. {
  8174. enum perf_event_active_state parent_state = parent_event->state;
  8175. struct perf_event *child_event;
  8176. unsigned long flags;
  8177. /*
  8178. * Instead of creating recursive hierarchies of events,
  8179. * we link inherited events back to the original parent,
  8180. * which has a filp for sure, which we use as the reference
  8181. * count:
  8182. */
  8183. if (parent_event->parent)
  8184. parent_event = parent_event->parent;
  8185. child_event = perf_event_alloc(&parent_event->attr,
  8186. parent_event->cpu,
  8187. child,
  8188. group_leader, parent_event,
  8189. NULL, NULL, -1);
  8190. if (IS_ERR(child_event))
  8191. return child_event;
  8192. /*
  8193. * is_orphaned_event() and list_add_tail(&parent_event->child_list)
  8194. * must be under the same lock in order to serialize against
  8195. * perf_event_release_kernel(), such that either we must observe
  8196. * is_orphaned_event() or they will observe us on the child_list.
  8197. */
  8198. mutex_lock(&parent_event->child_mutex);
  8199. if (is_orphaned_event(parent_event) ||
  8200. !atomic_long_inc_not_zero(&parent_event->refcount)) {
  8201. mutex_unlock(&parent_event->child_mutex);
  8202. free_event(child_event);
  8203. return NULL;
  8204. }
  8205. get_ctx(child_ctx);
  8206. /*
  8207. * Make the child state follow the state of the parent event,
  8208. * not its attr.disabled bit. We hold the parent's mutex,
  8209. * so we won't race with perf_event_{en, dis}able_family.
  8210. */
  8211. if (parent_state >= PERF_EVENT_STATE_INACTIVE)
  8212. child_event->state = PERF_EVENT_STATE_INACTIVE;
  8213. else
  8214. child_event->state = PERF_EVENT_STATE_OFF;
  8215. if (parent_event->attr.freq) {
  8216. u64 sample_period = parent_event->hw.sample_period;
  8217. struct hw_perf_event *hwc = &child_event->hw;
  8218. hwc->sample_period = sample_period;
  8219. hwc->last_period = sample_period;
  8220. local64_set(&hwc->period_left, sample_period);
  8221. }
  8222. child_event->ctx = child_ctx;
  8223. child_event->overflow_handler = parent_event->overflow_handler;
  8224. child_event->overflow_handler_context
  8225. = parent_event->overflow_handler_context;
  8226. /*
  8227. * Precalculate sample_data sizes
  8228. */
  8229. perf_event__header_size(child_event);
  8230. perf_event__id_header_size(child_event);
  8231. /*
  8232. * Link it up in the child's context:
  8233. */
  8234. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  8235. add_event_to_ctx(child_event, child_ctx);
  8236. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  8237. /*
  8238. * Link this into the parent event's child list
  8239. */
  8240. list_add_tail(&child_event->child_list, &parent_event->child_list);
  8241. mutex_unlock(&parent_event->child_mutex);
  8242. return child_event;
  8243. }
  8244. static int inherit_group(struct perf_event *parent_event,
  8245. struct task_struct *parent,
  8246. struct perf_event_context *parent_ctx,
  8247. struct task_struct *child,
  8248. struct perf_event_context *child_ctx)
  8249. {
  8250. struct perf_event *leader;
  8251. struct perf_event *sub;
  8252. struct perf_event *child_ctr;
  8253. leader = inherit_event(parent_event, parent, parent_ctx,
  8254. child, NULL, child_ctx);
  8255. if (IS_ERR(leader))
  8256. return PTR_ERR(leader);
  8257. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  8258. child_ctr = inherit_event(sub, parent, parent_ctx,
  8259. child, leader, child_ctx);
  8260. if (IS_ERR(child_ctr))
  8261. return PTR_ERR(child_ctr);
  8262. }
  8263. return 0;
  8264. }
  8265. static int
  8266. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  8267. struct perf_event_context *parent_ctx,
  8268. struct task_struct *child, int ctxn,
  8269. int *inherited_all)
  8270. {
  8271. int ret;
  8272. struct perf_event_context *child_ctx;
  8273. if (!event->attr.inherit) {
  8274. *inherited_all = 0;
  8275. return 0;
  8276. }
  8277. child_ctx = child->perf_event_ctxp[ctxn];
  8278. if (!child_ctx) {
  8279. /*
  8280. * This is executed from the parent task context, so
  8281. * inherit events that have been marked for cloning.
  8282. * First allocate and initialize a context for the
  8283. * child.
  8284. */
  8285. child_ctx = alloc_perf_context(parent_ctx->pmu, child);
  8286. if (!child_ctx)
  8287. return -ENOMEM;
  8288. child->perf_event_ctxp[ctxn] = child_ctx;
  8289. }
  8290. ret = inherit_group(event, parent, parent_ctx,
  8291. child, child_ctx);
  8292. if (ret)
  8293. *inherited_all = 0;
  8294. return ret;
  8295. }
  8296. /*
  8297. * Initialize the perf_event context in task_struct
  8298. */
  8299. static int perf_event_init_context(struct task_struct *child, int ctxn)
  8300. {
  8301. struct perf_event_context *child_ctx, *parent_ctx;
  8302. struct perf_event_context *cloned_ctx;
  8303. struct perf_event *event;
  8304. struct task_struct *parent = current;
  8305. int inherited_all = 1;
  8306. unsigned long flags;
  8307. int ret = 0;
  8308. if (likely(!parent->perf_event_ctxp[ctxn]))
  8309. return 0;
  8310. /*
  8311. * If the parent's context is a clone, pin it so it won't get
  8312. * swapped under us.
  8313. */
  8314. parent_ctx = perf_pin_task_context(parent, ctxn);
  8315. if (!parent_ctx)
  8316. return 0;
  8317. /*
  8318. * No need to check if parent_ctx != NULL here; since we saw
  8319. * it non-NULL earlier, the only reason for it to become NULL
  8320. * is if we exit, and since we're currently in the middle of
  8321. * a fork we can't be exiting at the same time.
  8322. */
  8323. /*
  8324. * Lock the parent list. No need to lock the child - not PID
  8325. * hashed yet and not running, so nobody can access it.
  8326. */
  8327. mutex_lock(&parent_ctx->mutex);
  8328. /*
  8329. * We dont have to disable NMIs - we are only looking at
  8330. * the list, not manipulating it:
  8331. */
  8332. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  8333. ret = inherit_task_group(event, parent, parent_ctx,
  8334. child, ctxn, &inherited_all);
  8335. if (ret)
  8336. break;
  8337. }
  8338. /*
  8339. * We can't hold ctx->lock when iterating the ->flexible_group list due
  8340. * to allocations, but we need to prevent rotation because
  8341. * rotate_ctx() will change the list from interrupt context.
  8342. */
  8343. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  8344. parent_ctx->rotate_disable = 1;
  8345. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  8346. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  8347. ret = inherit_task_group(event, parent, parent_ctx,
  8348. child, ctxn, &inherited_all);
  8349. if (ret)
  8350. break;
  8351. }
  8352. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  8353. parent_ctx->rotate_disable = 0;
  8354. child_ctx = child->perf_event_ctxp[ctxn];
  8355. if (child_ctx && inherited_all) {
  8356. /*
  8357. * Mark the child context as a clone of the parent
  8358. * context, or of whatever the parent is a clone of.
  8359. *
  8360. * Note that if the parent is a clone, the holding of
  8361. * parent_ctx->lock avoids it from being uncloned.
  8362. */
  8363. cloned_ctx = parent_ctx->parent_ctx;
  8364. if (cloned_ctx) {
  8365. child_ctx->parent_ctx = cloned_ctx;
  8366. child_ctx->parent_gen = parent_ctx->parent_gen;
  8367. } else {
  8368. child_ctx->parent_ctx = parent_ctx;
  8369. child_ctx->parent_gen = parent_ctx->generation;
  8370. }
  8371. get_ctx(child_ctx->parent_ctx);
  8372. }
  8373. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  8374. mutex_unlock(&parent_ctx->mutex);
  8375. perf_unpin_context(parent_ctx);
  8376. put_ctx(parent_ctx);
  8377. return ret;
  8378. }
  8379. /*
  8380. * Initialize the perf_event context in task_struct
  8381. */
  8382. int perf_event_init_task(struct task_struct *child)
  8383. {
  8384. int ctxn, ret;
  8385. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  8386. mutex_init(&child->perf_event_mutex);
  8387. INIT_LIST_HEAD(&child->perf_event_list);
  8388. for_each_task_context_nr(ctxn) {
  8389. ret = perf_event_init_context(child, ctxn);
  8390. if (ret) {
  8391. perf_event_free_task(child);
  8392. return ret;
  8393. }
  8394. }
  8395. return 0;
  8396. }
  8397. static void __init perf_event_init_all_cpus(void)
  8398. {
  8399. struct swevent_htable *swhash;
  8400. int cpu;
  8401. for_each_possible_cpu(cpu) {
  8402. swhash = &per_cpu(swevent_htable, cpu);
  8403. mutex_init(&swhash->hlist_mutex);
  8404. INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
  8405. }
  8406. }
  8407. static void perf_event_init_cpu(int cpu)
  8408. {
  8409. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  8410. mutex_lock(&swhash->hlist_mutex);
  8411. if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
  8412. struct swevent_hlist *hlist;
  8413. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  8414. WARN_ON(!hlist);
  8415. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  8416. }
  8417. mutex_unlock(&swhash->hlist_mutex);
  8418. }
  8419. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
  8420. static void __perf_event_exit_context(void *__info)
  8421. {
  8422. struct perf_event_context *ctx = __info;
  8423. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  8424. struct perf_event *event;
  8425. raw_spin_lock(&ctx->lock);
  8426. list_for_each_entry(event, &ctx->event_list, event_entry)
  8427. __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
  8428. raw_spin_unlock(&ctx->lock);
  8429. }
  8430. static void perf_event_exit_cpu_context(int cpu)
  8431. {
  8432. struct perf_event_context *ctx;
  8433. struct pmu *pmu;
  8434. int idx;
  8435. idx = srcu_read_lock(&pmus_srcu);
  8436. list_for_each_entry_rcu(pmu, &pmus, entry) {
  8437. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  8438. mutex_lock(&ctx->mutex);
  8439. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  8440. mutex_unlock(&ctx->mutex);
  8441. }
  8442. srcu_read_unlock(&pmus_srcu, idx);
  8443. }
  8444. static void perf_event_exit_cpu(int cpu)
  8445. {
  8446. perf_event_exit_cpu_context(cpu);
  8447. }
  8448. #else
  8449. static inline void perf_event_exit_cpu(int cpu) { }
  8450. #endif
  8451. static int
  8452. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  8453. {
  8454. int cpu;
  8455. for_each_online_cpu(cpu)
  8456. perf_event_exit_cpu(cpu);
  8457. return NOTIFY_OK;
  8458. }
  8459. /*
  8460. * Run the perf reboot notifier at the very last possible moment so that
  8461. * the generic watchdog code runs as long as possible.
  8462. */
  8463. static struct notifier_block perf_reboot_notifier = {
  8464. .notifier_call = perf_reboot,
  8465. .priority = INT_MIN,
  8466. };
  8467. static int
  8468. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  8469. {
  8470. unsigned int cpu = (long)hcpu;
  8471. switch (action & ~CPU_TASKS_FROZEN) {
  8472. case CPU_UP_PREPARE:
  8473. /*
  8474. * This must be done before the CPU comes alive, because the
  8475. * moment we can run tasks we can encounter (software) events.
  8476. *
  8477. * Specifically, someone can have inherited events on kthreadd
  8478. * or a pre-existing worker thread that gets re-bound.
  8479. */
  8480. perf_event_init_cpu(cpu);
  8481. break;
  8482. case CPU_DOWN_PREPARE:
  8483. /*
  8484. * This must be done before the CPU dies because after that an
  8485. * active event might want to IPI the CPU and that'll not work
  8486. * so great for dead CPUs.
  8487. *
  8488. * XXX smp_call_function_single() return -ENXIO without a warn
  8489. * so we could possibly deal with this.
  8490. *
  8491. * This is safe against new events arriving because
  8492. * sys_perf_event_open() serializes against hotplug using
  8493. * get_online_cpus().
  8494. */
  8495. perf_event_exit_cpu(cpu);
  8496. break;
  8497. default:
  8498. break;
  8499. }
  8500. return NOTIFY_OK;
  8501. }
  8502. void __init perf_event_init(void)
  8503. {
  8504. int ret;
  8505. idr_init(&pmu_idr);
  8506. perf_event_init_all_cpus();
  8507. init_srcu_struct(&pmus_srcu);
  8508. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  8509. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  8510. perf_pmu_register(&perf_task_clock, NULL, -1);
  8511. perf_tp_register();
  8512. perf_cpu_notifier(perf_cpu_notify);
  8513. register_reboot_notifier(&perf_reboot_notifier);
  8514. ret = init_hw_breakpoint();
  8515. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  8516. /*
  8517. * Build time assertion that we keep the data_head at the intended
  8518. * location. IOW, validation we got the __reserved[] size right.
  8519. */
  8520. BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
  8521. != 1024);
  8522. }
  8523. ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
  8524. char *page)
  8525. {
  8526. struct perf_pmu_events_attr *pmu_attr =
  8527. container_of(attr, struct perf_pmu_events_attr, attr);
  8528. if (pmu_attr->event_str)
  8529. return sprintf(page, "%s\n", pmu_attr->event_str);
  8530. return 0;
  8531. }
  8532. EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
  8533. static int __init perf_event_sysfs_init(void)
  8534. {
  8535. struct pmu *pmu;
  8536. int ret;
  8537. mutex_lock(&pmus_lock);
  8538. ret = bus_register(&pmu_bus);
  8539. if (ret)
  8540. goto unlock;
  8541. list_for_each_entry(pmu, &pmus, entry) {
  8542. if (!pmu->name || pmu->type < 0)
  8543. continue;
  8544. ret = pmu_dev_alloc(pmu);
  8545. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  8546. }
  8547. pmu_bus_running = 1;
  8548. ret = 0;
  8549. unlock:
  8550. mutex_unlock(&pmus_lock);
  8551. return ret;
  8552. }
  8553. device_initcall(perf_event_sysfs_init);
  8554. #ifdef CONFIG_CGROUP_PERF
  8555. static struct cgroup_subsys_state *
  8556. perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
  8557. {
  8558. struct perf_cgroup *jc;
  8559. jc = kzalloc(sizeof(*jc), GFP_KERNEL);
  8560. if (!jc)
  8561. return ERR_PTR(-ENOMEM);
  8562. jc->info = alloc_percpu(struct perf_cgroup_info);
  8563. if (!jc->info) {
  8564. kfree(jc);
  8565. return ERR_PTR(-ENOMEM);
  8566. }
  8567. return &jc->css;
  8568. }
  8569. static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
  8570. {
  8571. struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
  8572. free_percpu(jc->info);
  8573. kfree(jc);
  8574. }
  8575. static int __perf_cgroup_move(void *info)
  8576. {
  8577. struct task_struct *task = info;
  8578. rcu_read_lock();
  8579. perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
  8580. rcu_read_unlock();
  8581. return 0;
  8582. }
  8583. static void perf_cgroup_attach(struct cgroup_taskset *tset)
  8584. {
  8585. struct task_struct *task;
  8586. struct cgroup_subsys_state *css;
  8587. cgroup_taskset_for_each(task, css, tset)
  8588. task_function_call(task, __perf_cgroup_move, task);
  8589. }
  8590. struct cgroup_subsys perf_event_cgrp_subsys = {
  8591. .css_alloc = perf_cgroup_css_alloc,
  8592. .css_free = perf_cgroup_css_free,
  8593. .attach = perf_cgroup_attach,
  8594. };
  8595. #endif /* CONFIG_CGROUP_PERF */