core.c 175 KB

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