core.c 176 KB

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