core.c 175 KB

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