core.c 268 KB

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