core.c 264 KB

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