core.c 210 KB

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