core.c 176 KB

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