core.c 193 KB

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