core.c 257 KB

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