core.c 177 KB

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