core.c 174 KB

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