perf_event.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2009 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/file.h>
  16. #include <linux/poll.h>
  17. #include <linux/slab.h>
  18. #include <linux/hash.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/dcache.h>
  21. #include <linux/percpu.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/vmstat.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/hardirq.h>
  26. #include <linux/rculist.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/anon_inodes.h>
  30. #include <linux/kernel_stat.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/ftrace_event.h>
  33. #include <asm/irq_regs.h>
  34. /*
  35. * Each CPU has a list of per CPU events:
  36. */
  37. static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  38. int perf_max_events __read_mostly = 1;
  39. static int perf_reserved_percpu __read_mostly;
  40. static int perf_overcommit __read_mostly = 1;
  41. static atomic_t nr_events __read_mostly;
  42. static atomic_t nr_mmap_events __read_mostly;
  43. static atomic_t nr_comm_events __read_mostly;
  44. static atomic_t nr_task_events __read_mostly;
  45. /*
  46. * perf event paranoia level:
  47. * -1 - not paranoid at all
  48. * 0 - disallow raw tracepoint access for unpriv
  49. * 1 - disallow cpu events for unpriv
  50. * 2 - disallow kernel profiling for unpriv
  51. */
  52. int sysctl_perf_event_paranoid __read_mostly = 1;
  53. int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
  54. /*
  55. * max perf event sample rate
  56. */
  57. int sysctl_perf_event_sample_rate __read_mostly = 100000;
  58. static atomic64_t perf_event_id;
  59. /*
  60. * Lock for (sysadmin-configurable) event reservations:
  61. */
  62. static DEFINE_SPINLOCK(perf_resource_lock);
  63. void __weak perf_event_print_debug(void) { }
  64. void perf_pmu_disable(struct pmu *pmu)
  65. {
  66. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  67. if (!(*count)++)
  68. pmu->pmu_disable(pmu);
  69. }
  70. void perf_pmu_enable(struct pmu *pmu)
  71. {
  72. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  73. if (!--(*count))
  74. pmu->pmu_enable(pmu);
  75. }
  76. static void get_ctx(struct perf_event_context *ctx)
  77. {
  78. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  79. }
  80. static void free_ctx(struct rcu_head *head)
  81. {
  82. struct perf_event_context *ctx;
  83. ctx = container_of(head, struct perf_event_context, rcu_head);
  84. kfree(ctx);
  85. }
  86. static void put_ctx(struct perf_event_context *ctx)
  87. {
  88. if (atomic_dec_and_test(&ctx->refcount)) {
  89. if (ctx->parent_ctx)
  90. put_ctx(ctx->parent_ctx);
  91. if (ctx->task)
  92. put_task_struct(ctx->task);
  93. call_rcu(&ctx->rcu_head, free_ctx);
  94. }
  95. }
  96. static void unclone_ctx(struct perf_event_context *ctx)
  97. {
  98. if (ctx->parent_ctx) {
  99. put_ctx(ctx->parent_ctx);
  100. ctx->parent_ctx = NULL;
  101. }
  102. }
  103. /*
  104. * If we inherit events we want to return the parent event id
  105. * to userspace.
  106. */
  107. static u64 primary_event_id(struct perf_event *event)
  108. {
  109. u64 id = event->id;
  110. if (event->parent)
  111. id = event->parent->id;
  112. return id;
  113. }
  114. /*
  115. * Get the perf_event_context for a task and lock it.
  116. * This has to cope with with the fact that until it is locked,
  117. * the context could get moved to another task.
  118. */
  119. static struct perf_event_context *
  120. perf_lock_task_context(struct task_struct *task, unsigned long *flags)
  121. {
  122. struct perf_event_context *ctx;
  123. rcu_read_lock();
  124. retry:
  125. ctx = rcu_dereference(task->perf_event_ctxp);
  126. if (ctx) {
  127. /*
  128. * If this context is a clone of another, it might
  129. * get swapped for another underneath us by
  130. * perf_event_task_sched_out, though the
  131. * rcu_read_lock() protects us from any context
  132. * getting freed. Lock the context and check if it
  133. * got swapped before we could get the lock, and retry
  134. * if so. If we locked the right context, then it
  135. * can't get swapped on us any more.
  136. */
  137. raw_spin_lock_irqsave(&ctx->lock, *flags);
  138. if (ctx != rcu_dereference(task->perf_event_ctxp)) {
  139. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  140. goto retry;
  141. }
  142. if (!atomic_inc_not_zero(&ctx->refcount)) {
  143. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  144. ctx = NULL;
  145. }
  146. }
  147. rcu_read_unlock();
  148. return ctx;
  149. }
  150. /*
  151. * Get the context for a task and increment its pin_count so it
  152. * can't get swapped to another task. This also increments its
  153. * reference count so that the context can't get freed.
  154. */
  155. static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
  156. {
  157. struct perf_event_context *ctx;
  158. unsigned long flags;
  159. ctx = perf_lock_task_context(task, &flags);
  160. if (ctx) {
  161. ++ctx->pin_count;
  162. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  163. }
  164. return ctx;
  165. }
  166. static void perf_unpin_context(struct perf_event_context *ctx)
  167. {
  168. unsigned long flags;
  169. raw_spin_lock_irqsave(&ctx->lock, flags);
  170. --ctx->pin_count;
  171. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  172. put_ctx(ctx);
  173. }
  174. static inline u64 perf_clock(void)
  175. {
  176. return local_clock();
  177. }
  178. /*
  179. * Update the record of the current time in a context.
  180. */
  181. static void update_context_time(struct perf_event_context *ctx)
  182. {
  183. u64 now = perf_clock();
  184. ctx->time += now - ctx->timestamp;
  185. ctx->timestamp = now;
  186. }
  187. /*
  188. * Update the total_time_enabled and total_time_running fields for a event.
  189. */
  190. static void update_event_times(struct perf_event *event)
  191. {
  192. struct perf_event_context *ctx = event->ctx;
  193. u64 run_end;
  194. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  195. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  196. return;
  197. if (ctx->is_active)
  198. run_end = ctx->time;
  199. else
  200. run_end = event->tstamp_stopped;
  201. event->total_time_enabled = run_end - event->tstamp_enabled;
  202. if (event->state == PERF_EVENT_STATE_INACTIVE)
  203. run_end = event->tstamp_stopped;
  204. else
  205. run_end = ctx->time;
  206. event->total_time_running = run_end - event->tstamp_running;
  207. }
  208. /*
  209. * Update total_time_enabled and total_time_running for all events in a group.
  210. */
  211. static void update_group_times(struct perf_event *leader)
  212. {
  213. struct perf_event *event;
  214. update_event_times(leader);
  215. list_for_each_entry(event, &leader->sibling_list, group_entry)
  216. update_event_times(event);
  217. }
  218. static struct list_head *
  219. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  220. {
  221. if (event->attr.pinned)
  222. return &ctx->pinned_groups;
  223. else
  224. return &ctx->flexible_groups;
  225. }
  226. /*
  227. * Add a event from the lists for its context.
  228. * Must be called with ctx->mutex and ctx->lock held.
  229. */
  230. static void
  231. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  232. {
  233. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  234. event->attach_state |= PERF_ATTACH_CONTEXT;
  235. /*
  236. * If we're a stand alone event or group leader, we go to the context
  237. * list, group events are kept attached to the group so that
  238. * perf_group_detach can, at all times, locate all siblings.
  239. */
  240. if (event->group_leader == event) {
  241. struct list_head *list;
  242. if (is_software_event(event))
  243. event->group_flags |= PERF_GROUP_SOFTWARE;
  244. list = ctx_group_list(event, ctx);
  245. list_add_tail(&event->group_entry, list);
  246. }
  247. list_add_rcu(&event->event_entry, &ctx->event_list);
  248. ctx->nr_events++;
  249. if (event->attr.inherit_stat)
  250. ctx->nr_stat++;
  251. }
  252. static void perf_group_attach(struct perf_event *event)
  253. {
  254. struct perf_event *group_leader = event->group_leader;
  255. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
  256. event->attach_state |= PERF_ATTACH_GROUP;
  257. if (group_leader == event)
  258. return;
  259. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  260. !is_software_event(event))
  261. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  262. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  263. group_leader->nr_siblings++;
  264. }
  265. /*
  266. * Remove a event from the lists for its context.
  267. * Must be called with ctx->mutex and ctx->lock held.
  268. */
  269. static void
  270. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  271. {
  272. /*
  273. * We can have double detach due to exit/hot-unplug + close.
  274. */
  275. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  276. return;
  277. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  278. ctx->nr_events--;
  279. if (event->attr.inherit_stat)
  280. ctx->nr_stat--;
  281. list_del_rcu(&event->event_entry);
  282. if (event->group_leader == event)
  283. list_del_init(&event->group_entry);
  284. update_group_times(event);
  285. /*
  286. * If event was in error state, then keep it
  287. * that way, otherwise bogus counts will be
  288. * returned on read(). The only way to get out
  289. * of error state is by explicit re-enabling
  290. * of the event
  291. */
  292. if (event->state > PERF_EVENT_STATE_OFF)
  293. event->state = PERF_EVENT_STATE_OFF;
  294. }
  295. static void perf_group_detach(struct perf_event *event)
  296. {
  297. struct perf_event *sibling, *tmp;
  298. struct list_head *list = NULL;
  299. /*
  300. * We can have double detach due to exit/hot-unplug + close.
  301. */
  302. if (!(event->attach_state & PERF_ATTACH_GROUP))
  303. return;
  304. event->attach_state &= ~PERF_ATTACH_GROUP;
  305. /*
  306. * If this is a sibling, remove it from its group.
  307. */
  308. if (event->group_leader != event) {
  309. list_del_init(&event->group_entry);
  310. event->group_leader->nr_siblings--;
  311. return;
  312. }
  313. if (!list_empty(&event->group_entry))
  314. list = &event->group_entry;
  315. /*
  316. * If this was a group event with sibling events then
  317. * upgrade the siblings to singleton events by adding them
  318. * to whatever list we are on.
  319. */
  320. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  321. if (list)
  322. list_move_tail(&sibling->group_entry, list);
  323. sibling->group_leader = sibling;
  324. /* Inherit group flags from the previous leader */
  325. sibling->group_flags = event->group_flags;
  326. }
  327. }
  328. static inline int
  329. event_filter_match(struct perf_event *event)
  330. {
  331. return event->cpu == -1 || event->cpu == smp_processor_id();
  332. }
  333. static void
  334. event_sched_out(struct perf_event *event,
  335. struct perf_cpu_context *cpuctx,
  336. struct perf_event_context *ctx)
  337. {
  338. u64 delta;
  339. /*
  340. * An event which could not be activated because of
  341. * filter mismatch still needs to have its timings
  342. * maintained, otherwise bogus information is return
  343. * via read() for time_enabled, time_running:
  344. */
  345. if (event->state == PERF_EVENT_STATE_INACTIVE
  346. && !event_filter_match(event)) {
  347. delta = ctx->time - event->tstamp_stopped;
  348. event->tstamp_running += delta;
  349. event->tstamp_stopped = ctx->time;
  350. }
  351. if (event->state != PERF_EVENT_STATE_ACTIVE)
  352. return;
  353. event->state = PERF_EVENT_STATE_INACTIVE;
  354. if (event->pending_disable) {
  355. event->pending_disable = 0;
  356. event->state = PERF_EVENT_STATE_OFF;
  357. }
  358. event->tstamp_stopped = ctx->time;
  359. event->pmu->del(event, 0);
  360. event->oncpu = -1;
  361. if (!is_software_event(event))
  362. cpuctx->active_oncpu--;
  363. ctx->nr_active--;
  364. if (event->attr.exclusive || !cpuctx->active_oncpu)
  365. cpuctx->exclusive = 0;
  366. }
  367. static void
  368. group_sched_out(struct perf_event *group_event,
  369. struct perf_cpu_context *cpuctx,
  370. struct perf_event_context *ctx)
  371. {
  372. struct perf_event *event;
  373. int state = group_event->state;
  374. event_sched_out(group_event, cpuctx, ctx);
  375. /*
  376. * Schedule out siblings (if any):
  377. */
  378. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  379. event_sched_out(event, cpuctx, ctx);
  380. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  381. cpuctx->exclusive = 0;
  382. }
  383. /*
  384. * Cross CPU call to remove a performance event
  385. *
  386. * We disable the event on the hardware level first. After that we
  387. * remove it from the context list.
  388. */
  389. static void __perf_event_remove_from_context(void *info)
  390. {
  391. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  392. struct perf_event *event = info;
  393. struct perf_event_context *ctx = event->ctx;
  394. /*
  395. * If this is a task context, we need to check whether it is
  396. * the current task context of this cpu. If not it has been
  397. * scheduled out before the smp call arrived.
  398. */
  399. if (ctx->task && cpuctx->task_ctx != ctx)
  400. return;
  401. raw_spin_lock(&ctx->lock);
  402. event_sched_out(event, cpuctx, ctx);
  403. list_del_event(event, ctx);
  404. if (!ctx->task) {
  405. /*
  406. * Allow more per task events with respect to the
  407. * reservation:
  408. */
  409. cpuctx->max_pertask =
  410. min(perf_max_events - ctx->nr_events,
  411. perf_max_events - perf_reserved_percpu);
  412. }
  413. raw_spin_unlock(&ctx->lock);
  414. }
  415. /*
  416. * Remove the event from a task's (or a CPU's) list of events.
  417. *
  418. * Must be called with ctx->mutex held.
  419. *
  420. * CPU events are removed with a smp call. For task events we only
  421. * call when the task is on a CPU.
  422. *
  423. * If event->ctx is a cloned context, callers must make sure that
  424. * every task struct that event->ctx->task could possibly point to
  425. * remains valid. This is OK when called from perf_release since
  426. * that only calls us on the top-level context, which can't be a clone.
  427. * When called from perf_event_exit_task, it's OK because the
  428. * context has been detached from its task.
  429. */
  430. static void perf_event_remove_from_context(struct perf_event *event)
  431. {
  432. struct perf_event_context *ctx = event->ctx;
  433. struct task_struct *task = ctx->task;
  434. if (!task) {
  435. /*
  436. * Per cpu events are removed via an smp call and
  437. * the removal is always successful.
  438. */
  439. smp_call_function_single(event->cpu,
  440. __perf_event_remove_from_context,
  441. event, 1);
  442. return;
  443. }
  444. retry:
  445. task_oncpu_function_call(task, __perf_event_remove_from_context,
  446. event);
  447. raw_spin_lock_irq(&ctx->lock);
  448. /*
  449. * If the context is active we need to retry the smp call.
  450. */
  451. if (ctx->nr_active && !list_empty(&event->group_entry)) {
  452. raw_spin_unlock_irq(&ctx->lock);
  453. goto retry;
  454. }
  455. /*
  456. * The lock prevents that this context is scheduled in so we
  457. * can remove the event safely, if the call above did not
  458. * succeed.
  459. */
  460. if (!list_empty(&event->group_entry))
  461. list_del_event(event, ctx);
  462. raw_spin_unlock_irq(&ctx->lock);
  463. }
  464. /*
  465. * Cross CPU call to disable a performance event
  466. */
  467. static void __perf_event_disable(void *info)
  468. {
  469. struct perf_event *event = info;
  470. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  471. struct perf_event_context *ctx = event->ctx;
  472. /*
  473. * If this is a per-task event, need to check whether this
  474. * event's task is the current task on this cpu.
  475. */
  476. if (ctx->task && cpuctx->task_ctx != ctx)
  477. return;
  478. raw_spin_lock(&ctx->lock);
  479. /*
  480. * If the event is on, turn it off.
  481. * If it is in error state, leave it in error state.
  482. */
  483. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  484. update_context_time(ctx);
  485. update_group_times(event);
  486. if (event == event->group_leader)
  487. group_sched_out(event, cpuctx, ctx);
  488. else
  489. event_sched_out(event, cpuctx, ctx);
  490. event->state = PERF_EVENT_STATE_OFF;
  491. }
  492. raw_spin_unlock(&ctx->lock);
  493. }
  494. /*
  495. * Disable a event.
  496. *
  497. * If event->ctx is a cloned context, callers must make sure that
  498. * every task struct that event->ctx->task could possibly point to
  499. * remains valid. This condition is satisifed when called through
  500. * perf_event_for_each_child or perf_event_for_each because they
  501. * hold the top-level event's child_mutex, so any descendant that
  502. * goes to exit will block in sync_child_event.
  503. * When called from perf_pending_event it's OK because event->ctx
  504. * is the current context on this CPU and preemption is disabled,
  505. * hence we can't get into perf_event_task_sched_out for this context.
  506. */
  507. void perf_event_disable(struct perf_event *event)
  508. {
  509. struct perf_event_context *ctx = event->ctx;
  510. struct task_struct *task = ctx->task;
  511. if (!task) {
  512. /*
  513. * Disable the event on the cpu that it's on
  514. */
  515. smp_call_function_single(event->cpu, __perf_event_disable,
  516. event, 1);
  517. return;
  518. }
  519. retry:
  520. task_oncpu_function_call(task, __perf_event_disable, event);
  521. raw_spin_lock_irq(&ctx->lock);
  522. /*
  523. * If the event is still active, we need to retry the cross-call.
  524. */
  525. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  526. raw_spin_unlock_irq(&ctx->lock);
  527. goto retry;
  528. }
  529. /*
  530. * Since we have the lock this context can't be scheduled
  531. * in, so we can change the state safely.
  532. */
  533. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  534. update_group_times(event);
  535. event->state = PERF_EVENT_STATE_OFF;
  536. }
  537. raw_spin_unlock_irq(&ctx->lock);
  538. }
  539. static int
  540. event_sched_in(struct perf_event *event,
  541. struct perf_cpu_context *cpuctx,
  542. struct perf_event_context *ctx)
  543. {
  544. if (event->state <= PERF_EVENT_STATE_OFF)
  545. return 0;
  546. event->state = PERF_EVENT_STATE_ACTIVE;
  547. event->oncpu = smp_processor_id();
  548. /*
  549. * The new state must be visible before we turn it on in the hardware:
  550. */
  551. smp_wmb();
  552. if (event->pmu->add(event, PERF_EF_START)) {
  553. event->state = PERF_EVENT_STATE_INACTIVE;
  554. event->oncpu = -1;
  555. return -EAGAIN;
  556. }
  557. event->tstamp_running += ctx->time - event->tstamp_stopped;
  558. if (!is_software_event(event))
  559. cpuctx->active_oncpu++;
  560. ctx->nr_active++;
  561. if (event->attr.exclusive)
  562. cpuctx->exclusive = 1;
  563. return 0;
  564. }
  565. static int
  566. group_sched_in(struct perf_event *group_event,
  567. struct perf_cpu_context *cpuctx,
  568. struct perf_event_context *ctx)
  569. {
  570. struct perf_event *event, *partial_group = NULL;
  571. struct pmu *pmu = group_event->pmu;
  572. if (group_event->state == PERF_EVENT_STATE_OFF)
  573. return 0;
  574. pmu->start_txn(pmu);
  575. if (event_sched_in(group_event, cpuctx, ctx)) {
  576. pmu->cancel_txn(pmu);
  577. return -EAGAIN;
  578. }
  579. /*
  580. * Schedule in siblings as one group (if any):
  581. */
  582. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  583. if (event_sched_in(event, cpuctx, ctx)) {
  584. partial_group = event;
  585. goto group_error;
  586. }
  587. }
  588. if (!pmu->commit_txn(pmu))
  589. return 0;
  590. group_error:
  591. /*
  592. * Groups can be scheduled in as one unit only, so undo any
  593. * partial group before returning:
  594. */
  595. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  596. if (event == partial_group)
  597. break;
  598. event_sched_out(event, cpuctx, ctx);
  599. }
  600. event_sched_out(group_event, cpuctx, ctx);
  601. pmu->cancel_txn(pmu);
  602. return -EAGAIN;
  603. }
  604. /*
  605. * Work out whether we can put this event group on the CPU now.
  606. */
  607. static int group_can_go_on(struct perf_event *event,
  608. struct perf_cpu_context *cpuctx,
  609. int can_add_hw)
  610. {
  611. /*
  612. * Groups consisting entirely of software events can always go on.
  613. */
  614. if (event->group_flags & PERF_GROUP_SOFTWARE)
  615. return 1;
  616. /*
  617. * If an exclusive group is already on, no other hardware
  618. * events can go on.
  619. */
  620. if (cpuctx->exclusive)
  621. return 0;
  622. /*
  623. * If this group is exclusive and there are already
  624. * events on the CPU, it can't go on.
  625. */
  626. if (event->attr.exclusive && cpuctx->active_oncpu)
  627. return 0;
  628. /*
  629. * Otherwise, try to add it if all previous groups were able
  630. * to go on.
  631. */
  632. return can_add_hw;
  633. }
  634. static void add_event_to_ctx(struct perf_event *event,
  635. struct perf_event_context *ctx)
  636. {
  637. list_add_event(event, ctx);
  638. perf_group_attach(event);
  639. event->tstamp_enabled = ctx->time;
  640. event->tstamp_running = ctx->time;
  641. event->tstamp_stopped = ctx->time;
  642. }
  643. /*
  644. * Cross CPU call to install and enable a performance event
  645. *
  646. * Must be called with ctx->mutex held
  647. */
  648. static void __perf_install_in_context(void *info)
  649. {
  650. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  651. struct perf_event *event = info;
  652. struct perf_event_context *ctx = event->ctx;
  653. struct perf_event *leader = event->group_leader;
  654. int err;
  655. /*
  656. * If this is a task context, we need to check whether it is
  657. * the current task context of this cpu. If not it has been
  658. * scheduled out before the smp call arrived.
  659. * Or possibly this is the right context but it isn't
  660. * on this cpu because it had no events.
  661. */
  662. if (ctx->task && cpuctx->task_ctx != ctx) {
  663. if (cpuctx->task_ctx || ctx->task != current)
  664. return;
  665. cpuctx->task_ctx = ctx;
  666. }
  667. raw_spin_lock(&ctx->lock);
  668. ctx->is_active = 1;
  669. update_context_time(ctx);
  670. add_event_to_ctx(event, ctx);
  671. if (event->cpu != -1 && event->cpu != smp_processor_id())
  672. goto unlock;
  673. /*
  674. * Don't put the event on if it is disabled or if
  675. * it is in a group and the group isn't on.
  676. */
  677. if (event->state != PERF_EVENT_STATE_INACTIVE ||
  678. (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
  679. goto unlock;
  680. /*
  681. * An exclusive event can't go on if there are already active
  682. * hardware events, and no hardware event can go on if there
  683. * is already an exclusive event on.
  684. */
  685. if (!group_can_go_on(event, cpuctx, 1))
  686. err = -EEXIST;
  687. else
  688. err = event_sched_in(event, cpuctx, ctx);
  689. if (err) {
  690. /*
  691. * This event couldn't go on. If it is in a group
  692. * then we have to pull the whole group off.
  693. * If the event group is pinned then put it in error state.
  694. */
  695. if (leader != event)
  696. group_sched_out(leader, cpuctx, ctx);
  697. if (leader->attr.pinned) {
  698. update_group_times(leader);
  699. leader->state = PERF_EVENT_STATE_ERROR;
  700. }
  701. }
  702. if (!err && !ctx->task && cpuctx->max_pertask)
  703. cpuctx->max_pertask--;
  704. unlock:
  705. raw_spin_unlock(&ctx->lock);
  706. }
  707. /*
  708. * Attach a performance event to a context
  709. *
  710. * First we add the event to the list with the hardware enable bit
  711. * in event->hw_config cleared.
  712. *
  713. * If the event is attached to a task which is on a CPU we use a smp
  714. * call to enable it in the task context. The task might have been
  715. * scheduled away, but we check this in the smp call again.
  716. *
  717. * Must be called with ctx->mutex held.
  718. */
  719. static void
  720. perf_install_in_context(struct perf_event_context *ctx,
  721. struct perf_event *event,
  722. int cpu)
  723. {
  724. struct task_struct *task = ctx->task;
  725. if (!task) {
  726. /*
  727. * Per cpu events are installed via an smp call and
  728. * the install is always successful.
  729. */
  730. smp_call_function_single(cpu, __perf_install_in_context,
  731. event, 1);
  732. return;
  733. }
  734. retry:
  735. task_oncpu_function_call(task, __perf_install_in_context,
  736. event);
  737. raw_spin_lock_irq(&ctx->lock);
  738. /*
  739. * we need to retry the smp call.
  740. */
  741. if (ctx->is_active && list_empty(&event->group_entry)) {
  742. raw_spin_unlock_irq(&ctx->lock);
  743. goto retry;
  744. }
  745. /*
  746. * The lock prevents that this context is scheduled in so we
  747. * can add the event safely, if it the call above did not
  748. * succeed.
  749. */
  750. if (list_empty(&event->group_entry))
  751. add_event_to_ctx(event, ctx);
  752. raw_spin_unlock_irq(&ctx->lock);
  753. }
  754. /*
  755. * Put a event into inactive state and update time fields.
  756. * Enabling the leader of a group effectively enables all
  757. * the group members that aren't explicitly disabled, so we
  758. * have to update their ->tstamp_enabled also.
  759. * Note: this works for group members as well as group leaders
  760. * since the non-leader members' sibling_lists will be empty.
  761. */
  762. static void __perf_event_mark_enabled(struct perf_event *event,
  763. struct perf_event_context *ctx)
  764. {
  765. struct perf_event *sub;
  766. event->state = PERF_EVENT_STATE_INACTIVE;
  767. event->tstamp_enabled = ctx->time - event->total_time_enabled;
  768. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  769. if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
  770. sub->tstamp_enabled =
  771. ctx->time - sub->total_time_enabled;
  772. }
  773. }
  774. }
  775. /*
  776. * Cross CPU call to enable a performance event
  777. */
  778. static void __perf_event_enable(void *info)
  779. {
  780. struct perf_event *event = info;
  781. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  782. struct perf_event_context *ctx = event->ctx;
  783. struct perf_event *leader = event->group_leader;
  784. int err;
  785. /*
  786. * If this is a per-task event, need to check whether this
  787. * event's task is the current task on this cpu.
  788. */
  789. if (ctx->task && cpuctx->task_ctx != ctx) {
  790. if (cpuctx->task_ctx || ctx->task != current)
  791. return;
  792. cpuctx->task_ctx = ctx;
  793. }
  794. raw_spin_lock(&ctx->lock);
  795. ctx->is_active = 1;
  796. update_context_time(ctx);
  797. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  798. goto unlock;
  799. __perf_event_mark_enabled(event, ctx);
  800. if (event->cpu != -1 && event->cpu != smp_processor_id())
  801. goto unlock;
  802. /*
  803. * If the event is in a group and isn't the group leader,
  804. * then don't put it on unless the group is on.
  805. */
  806. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  807. goto unlock;
  808. if (!group_can_go_on(event, cpuctx, 1)) {
  809. err = -EEXIST;
  810. } else {
  811. if (event == leader)
  812. err = group_sched_in(event, cpuctx, ctx);
  813. else
  814. err = event_sched_in(event, cpuctx, ctx);
  815. }
  816. if (err) {
  817. /*
  818. * If this event can't go on and it's part of a
  819. * group, then the whole group has to come off.
  820. */
  821. if (leader != event)
  822. group_sched_out(leader, cpuctx, ctx);
  823. if (leader->attr.pinned) {
  824. update_group_times(leader);
  825. leader->state = PERF_EVENT_STATE_ERROR;
  826. }
  827. }
  828. unlock:
  829. raw_spin_unlock(&ctx->lock);
  830. }
  831. /*
  832. * Enable a event.
  833. *
  834. * If event->ctx is a cloned context, callers must make sure that
  835. * every task struct that event->ctx->task could possibly point to
  836. * remains valid. This condition is satisfied when called through
  837. * perf_event_for_each_child or perf_event_for_each as described
  838. * for perf_event_disable.
  839. */
  840. void perf_event_enable(struct perf_event *event)
  841. {
  842. struct perf_event_context *ctx = event->ctx;
  843. struct task_struct *task = ctx->task;
  844. if (!task) {
  845. /*
  846. * Enable the event on the cpu that it's on
  847. */
  848. smp_call_function_single(event->cpu, __perf_event_enable,
  849. event, 1);
  850. return;
  851. }
  852. raw_spin_lock_irq(&ctx->lock);
  853. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  854. goto out;
  855. /*
  856. * If the event is in error state, clear that first.
  857. * That way, if we see the event in error state below, we
  858. * know that it has gone back into error state, as distinct
  859. * from the task having been scheduled away before the
  860. * cross-call arrived.
  861. */
  862. if (event->state == PERF_EVENT_STATE_ERROR)
  863. event->state = PERF_EVENT_STATE_OFF;
  864. retry:
  865. raw_spin_unlock_irq(&ctx->lock);
  866. task_oncpu_function_call(task, __perf_event_enable, event);
  867. raw_spin_lock_irq(&ctx->lock);
  868. /*
  869. * If the context is active and the event is still off,
  870. * we need to retry the cross-call.
  871. */
  872. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
  873. goto retry;
  874. /*
  875. * Since we have the lock this context can't be scheduled
  876. * in, so we can change the state safely.
  877. */
  878. if (event->state == PERF_EVENT_STATE_OFF)
  879. __perf_event_mark_enabled(event, ctx);
  880. out:
  881. raw_spin_unlock_irq(&ctx->lock);
  882. }
  883. static int perf_event_refresh(struct perf_event *event, int refresh)
  884. {
  885. /*
  886. * not supported on inherited events
  887. */
  888. if (event->attr.inherit)
  889. return -EINVAL;
  890. atomic_add(refresh, &event->event_limit);
  891. perf_event_enable(event);
  892. return 0;
  893. }
  894. enum event_type_t {
  895. EVENT_FLEXIBLE = 0x1,
  896. EVENT_PINNED = 0x2,
  897. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  898. };
  899. static void ctx_sched_out(struct perf_event_context *ctx,
  900. struct perf_cpu_context *cpuctx,
  901. enum event_type_t event_type)
  902. {
  903. struct perf_event *event;
  904. raw_spin_lock(&ctx->lock);
  905. ctx->is_active = 0;
  906. if (likely(!ctx->nr_events))
  907. goto out;
  908. update_context_time(ctx);
  909. if (!ctx->nr_active)
  910. goto out;
  911. if (event_type & EVENT_PINNED) {
  912. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  913. group_sched_out(event, cpuctx, ctx);
  914. }
  915. if (event_type & EVENT_FLEXIBLE) {
  916. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  917. group_sched_out(event, cpuctx, ctx);
  918. }
  919. out:
  920. raw_spin_unlock(&ctx->lock);
  921. }
  922. /*
  923. * Test whether two contexts are equivalent, i.e. whether they
  924. * have both been cloned from the same version of the same context
  925. * and they both have the same number of enabled events.
  926. * If the number of enabled events is the same, then the set
  927. * of enabled events should be the same, because these are both
  928. * inherited contexts, therefore we can't access individual events
  929. * in them directly with an fd; we can only enable/disable all
  930. * events via prctl, or enable/disable all events in a family
  931. * via ioctl, which will have the same effect on both contexts.
  932. */
  933. static int context_equiv(struct perf_event_context *ctx1,
  934. struct perf_event_context *ctx2)
  935. {
  936. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  937. && ctx1->parent_gen == ctx2->parent_gen
  938. && !ctx1->pin_count && !ctx2->pin_count;
  939. }
  940. static void __perf_event_sync_stat(struct perf_event *event,
  941. struct perf_event *next_event)
  942. {
  943. u64 value;
  944. if (!event->attr.inherit_stat)
  945. return;
  946. /*
  947. * Update the event value, we cannot use perf_event_read()
  948. * because we're in the middle of a context switch and have IRQs
  949. * disabled, which upsets smp_call_function_single(), however
  950. * we know the event must be on the current CPU, therefore we
  951. * don't need to use it.
  952. */
  953. switch (event->state) {
  954. case PERF_EVENT_STATE_ACTIVE:
  955. event->pmu->read(event);
  956. /* fall-through */
  957. case PERF_EVENT_STATE_INACTIVE:
  958. update_event_times(event);
  959. break;
  960. default:
  961. break;
  962. }
  963. /*
  964. * In order to keep per-task stats reliable we need to flip the event
  965. * values when we flip the contexts.
  966. */
  967. value = local64_read(&next_event->count);
  968. value = local64_xchg(&event->count, value);
  969. local64_set(&next_event->count, value);
  970. swap(event->total_time_enabled, next_event->total_time_enabled);
  971. swap(event->total_time_running, next_event->total_time_running);
  972. /*
  973. * Since we swizzled the values, update the user visible data too.
  974. */
  975. perf_event_update_userpage(event);
  976. perf_event_update_userpage(next_event);
  977. }
  978. #define list_next_entry(pos, member) \
  979. list_entry(pos->member.next, typeof(*pos), member)
  980. static void perf_event_sync_stat(struct perf_event_context *ctx,
  981. struct perf_event_context *next_ctx)
  982. {
  983. struct perf_event *event, *next_event;
  984. if (!ctx->nr_stat)
  985. return;
  986. update_context_time(ctx);
  987. event = list_first_entry(&ctx->event_list,
  988. struct perf_event, event_entry);
  989. next_event = list_first_entry(&next_ctx->event_list,
  990. struct perf_event, event_entry);
  991. while (&event->event_entry != &ctx->event_list &&
  992. &next_event->event_entry != &next_ctx->event_list) {
  993. __perf_event_sync_stat(event, next_event);
  994. event = list_next_entry(event, event_entry);
  995. next_event = list_next_entry(next_event, event_entry);
  996. }
  997. }
  998. /*
  999. * Called from scheduler to remove the events of the current task,
  1000. * with interrupts disabled.
  1001. *
  1002. * We stop each event and update the event value in event->count.
  1003. *
  1004. * This does not protect us against NMI, but disable()
  1005. * sets the disabled bit in the control field of event _before_
  1006. * accessing the event control register. If a NMI hits, then it will
  1007. * not restart the event.
  1008. */
  1009. void perf_event_task_sched_out(struct task_struct *task,
  1010. struct task_struct *next)
  1011. {
  1012. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1013. struct perf_event_context *ctx = task->perf_event_ctxp;
  1014. struct perf_event_context *next_ctx;
  1015. struct perf_event_context *parent;
  1016. int do_switch = 1;
  1017. perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
  1018. if (likely(!ctx || !cpuctx->task_ctx))
  1019. return;
  1020. rcu_read_lock();
  1021. parent = rcu_dereference(ctx->parent_ctx);
  1022. next_ctx = next->perf_event_ctxp;
  1023. if (parent && next_ctx &&
  1024. rcu_dereference(next_ctx->parent_ctx) == parent) {
  1025. /*
  1026. * Looks like the two contexts are clones, so we might be
  1027. * able to optimize the context switch. We lock both
  1028. * contexts and check that they are clones under the
  1029. * lock (including re-checking that neither has been
  1030. * uncloned in the meantime). It doesn't matter which
  1031. * order we take the locks because no other cpu could
  1032. * be trying to lock both of these tasks.
  1033. */
  1034. raw_spin_lock(&ctx->lock);
  1035. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  1036. if (context_equiv(ctx, next_ctx)) {
  1037. /*
  1038. * XXX do we need a memory barrier of sorts
  1039. * wrt to rcu_dereference() of perf_event_ctxp
  1040. */
  1041. task->perf_event_ctxp = next_ctx;
  1042. next->perf_event_ctxp = ctx;
  1043. ctx->task = next;
  1044. next_ctx->task = task;
  1045. do_switch = 0;
  1046. perf_event_sync_stat(ctx, next_ctx);
  1047. }
  1048. raw_spin_unlock(&next_ctx->lock);
  1049. raw_spin_unlock(&ctx->lock);
  1050. }
  1051. rcu_read_unlock();
  1052. if (do_switch) {
  1053. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1054. cpuctx->task_ctx = NULL;
  1055. }
  1056. }
  1057. static void task_ctx_sched_out(struct perf_event_context *ctx,
  1058. enum event_type_t event_type)
  1059. {
  1060. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1061. if (!cpuctx->task_ctx)
  1062. return;
  1063. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  1064. return;
  1065. ctx_sched_out(ctx, cpuctx, event_type);
  1066. cpuctx->task_ctx = NULL;
  1067. }
  1068. /*
  1069. * Called with IRQs disabled
  1070. */
  1071. static void __perf_event_task_sched_out(struct perf_event_context *ctx)
  1072. {
  1073. task_ctx_sched_out(ctx, EVENT_ALL);
  1074. }
  1075. /*
  1076. * Called with IRQs disabled
  1077. */
  1078. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  1079. enum event_type_t event_type)
  1080. {
  1081. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  1082. }
  1083. static void
  1084. ctx_pinned_sched_in(struct perf_event_context *ctx,
  1085. struct perf_cpu_context *cpuctx)
  1086. {
  1087. struct perf_event *event;
  1088. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1089. if (event->state <= PERF_EVENT_STATE_OFF)
  1090. continue;
  1091. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1092. continue;
  1093. if (group_can_go_on(event, cpuctx, 1))
  1094. group_sched_in(event, cpuctx, ctx);
  1095. /*
  1096. * If this pinned group hasn't been scheduled,
  1097. * put it in error state.
  1098. */
  1099. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1100. update_group_times(event);
  1101. event->state = PERF_EVENT_STATE_ERROR;
  1102. }
  1103. }
  1104. }
  1105. static void
  1106. ctx_flexible_sched_in(struct perf_event_context *ctx,
  1107. struct perf_cpu_context *cpuctx)
  1108. {
  1109. struct perf_event *event;
  1110. int can_add_hw = 1;
  1111. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1112. /* Ignore events in OFF or ERROR state */
  1113. if (event->state <= PERF_EVENT_STATE_OFF)
  1114. continue;
  1115. /*
  1116. * Listen to the 'cpu' scheduling filter constraint
  1117. * of events:
  1118. */
  1119. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1120. continue;
  1121. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  1122. if (group_sched_in(event, cpuctx, ctx))
  1123. can_add_hw = 0;
  1124. }
  1125. }
  1126. }
  1127. static void
  1128. ctx_sched_in(struct perf_event_context *ctx,
  1129. struct perf_cpu_context *cpuctx,
  1130. enum event_type_t event_type)
  1131. {
  1132. raw_spin_lock(&ctx->lock);
  1133. ctx->is_active = 1;
  1134. if (likely(!ctx->nr_events))
  1135. goto out;
  1136. ctx->timestamp = perf_clock();
  1137. /*
  1138. * First go through the list and put on any pinned groups
  1139. * in order to give them the best chance of going on.
  1140. */
  1141. if (event_type & EVENT_PINNED)
  1142. ctx_pinned_sched_in(ctx, cpuctx);
  1143. /* Then walk through the lower prio flexible groups */
  1144. if (event_type & EVENT_FLEXIBLE)
  1145. ctx_flexible_sched_in(ctx, cpuctx);
  1146. out:
  1147. raw_spin_unlock(&ctx->lock);
  1148. }
  1149. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  1150. enum event_type_t event_type)
  1151. {
  1152. struct perf_event_context *ctx = &cpuctx->ctx;
  1153. ctx_sched_in(ctx, cpuctx, event_type);
  1154. }
  1155. static void task_ctx_sched_in(struct task_struct *task,
  1156. enum event_type_t event_type)
  1157. {
  1158. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1159. struct perf_event_context *ctx = task->perf_event_ctxp;
  1160. if (likely(!ctx))
  1161. return;
  1162. if (cpuctx->task_ctx == ctx)
  1163. return;
  1164. ctx_sched_in(ctx, cpuctx, event_type);
  1165. cpuctx->task_ctx = ctx;
  1166. }
  1167. /*
  1168. * Called from scheduler to add the events of the current task
  1169. * with interrupts disabled.
  1170. *
  1171. * We restore the event value and then enable it.
  1172. *
  1173. * This does not protect us against NMI, but enable()
  1174. * sets the enabled bit in the control field of event _before_
  1175. * accessing the event control register. If a NMI hits, then it will
  1176. * keep the event running.
  1177. */
  1178. void perf_event_task_sched_in(struct task_struct *task)
  1179. {
  1180. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1181. struct perf_event_context *ctx = task->perf_event_ctxp;
  1182. if (likely(!ctx))
  1183. return;
  1184. if (cpuctx->task_ctx == ctx)
  1185. return;
  1186. /*
  1187. * We want to keep the following priority order:
  1188. * cpu pinned (that don't need to move), task pinned,
  1189. * cpu flexible, task flexible.
  1190. */
  1191. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1192. ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
  1193. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1194. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
  1195. cpuctx->task_ctx = ctx;
  1196. }
  1197. #define MAX_INTERRUPTS (~0ULL)
  1198. static void perf_log_throttle(struct perf_event *event, int enable);
  1199. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  1200. {
  1201. u64 frequency = event->attr.sample_freq;
  1202. u64 sec = NSEC_PER_SEC;
  1203. u64 divisor, dividend;
  1204. int count_fls, nsec_fls, frequency_fls, sec_fls;
  1205. count_fls = fls64(count);
  1206. nsec_fls = fls64(nsec);
  1207. frequency_fls = fls64(frequency);
  1208. sec_fls = 30;
  1209. /*
  1210. * We got @count in @nsec, with a target of sample_freq HZ
  1211. * the target period becomes:
  1212. *
  1213. * @count * 10^9
  1214. * period = -------------------
  1215. * @nsec * sample_freq
  1216. *
  1217. */
  1218. /*
  1219. * Reduce accuracy by one bit such that @a and @b converge
  1220. * to a similar magnitude.
  1221. */
  1222. #define REDUCE_FLS(a, b) \
  1223. do { \
  1224. if (a##_fls > b##_fls) { \
  1225. a >>= 1; \
  1226. a##_fls--; \
  1227. } else { \
  1228. b >>= 1; \
  1229. b##_fls--; \
  1230. } \
  1231. } while (0)
  1232. /*
  1233. * Reduce accuracy until either term fits in a u64, then proceed with
  1234. * the other, so that finally we can do a u64/u64 division.
  1235. */
  1236. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  1237. REDUCE_FLS(nsec, frequency);
  1238. REDUCE_FLS(sec, count);
  1239. }
  1240. if (count_fls + sec_fls > 64) {
  1241. divisor = nsec * frequency;
  1242. while (count_fls + sec_fls > 64) {
  1243. REDUCE_FLS(count, sec);
  1244. divisor >>= 1;
  1245. }
  1246. dividend = count * sec;
  1247. } else {
  1248. dividend = count * sec;
  1249. while (nsec_fls + frequency_fls > 64) {
  1250. REDUCE_FLS(nsec, frequency);
  1251. dividend >>= 1;
  1252. }
  1253. divisor = nsec * frequency;
  1254. }
  1255. if (!divisor)
  1256. return dividend;
  1257. return div64_u64(dividend, divisor);
  1258. }
  1259. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
  1260. {
  1261. struct hw_perf_event *hwc = &event->hw;
  1262. s64 period, sample_period;
  1263. s64 delta;
  1264. period = perf_calculate_period(event, nsec, count);
  1265. delta = (s64)(period - hwc->sample_period);
  1266. delta = (delta + 7) / 8; /* low pass filter */
  1267. sample_period = hwc->sample_period + delta;
  1268. if (!sample_period)
  1269. sample_period = 1;
  1270. hwc->sample_period = sample_period;
  1271. if (local64_read(&hwc->period_left) > 8*sample_period) {
  1272. event->pmu->stop(event, PERF_EF_UPDATE);
  1273. local64_set(&hwc->period_left, 0);
  1274. event->pmu->start(event, PERF_EF_RELOAD);
  1275. }
  1276. }
  1277. static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
  1278. {
  1279. struct perf_event *event;
  1280. struct hw_perf_event *hwc;
  1281. u64 interrupts, now;
  1282. s64 delta;
  1283. raw_spin_lock(&ctx->lock);
  1284. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  1285. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1286. continue;
  1287. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1288. continue;
  1289. hwc = &event->hw;
  1290. interrupts = hwc->interrupts;
  1291. hwc->interrupts = 0;
  1292. /*
  1293. * unthrottle events on the tick
  1294. */
  1295. if (interrupts == MAX_INTERRUPTS) {
  1296. perf_log_throttle(event, 1);
  1297. event->pmu->start(event, 0);
  1298. }
  1299. if (!event->attr.freq || !event->attr.sample_freq)
  1300. continue;
  1301. event->pmu->read(event);
  1302. now = local64_read(&event->count);
  1303. delta = now - hwc->freq_count_stamp;
  1304. hwc->freq_count_stamp = now;
  1305. if (delta > 0)
  1306. perf_adjust_period(event, TICK_NSEC, delta);
  1307. }
  1308. raw_spin_unlock(&ctx->lock);
  1309. }
  1310. /*
  1311. * Round-robin a context's events:
  1312. */
  1313. static void rotate_ctx(struct perf_event_context *ctx)
  1314. {
  1315. raw_spin_lock(&ctx->lock);
  1316. /* Rotate the first entry last of non-pinned groups */
  1317. list_rotate_left(&ctx->flexible_groups);
  1318. raw_spin_unlock(&ctx->lock);
  1319. }
  1320. void perf_event_task_tick(struct task_struct *curr)
  1321. {
  1322. struct perf_cpu_context *cpuctx;
  1323. struct perf_event_context *ctx;
  1324. int rotate = 0;
  1325. if (!atomic_read(&nr_events))
  1326. return;
  1327. cpuctx = &__get_cpu_var(perf_cpu_context);
  1328. if (cpuctx->ctx.nr_events &&
  1329. cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  1330. rotate = 1;
  1331. ctx = curr->perf_event_ctxp;
  1332. if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
  1333. rotate = 1;
  1334. perf_ctx_adjust_freq(&cpuctx->ctx);
  1335. if (ctx)
  1336. perf_ctx_adjust_freq(ctx);
  1337. if (!rotate)
  1338. return;
  1339. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1340. if (ctx)
  1341. task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
  1342. rotate_ctx(&cpuctx->ctx);
  1343. if (ctx)
  1344. rotate_ctx(ctx);
  1345. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1346. if (ctx)
  1347. task_ctx_sched_in(curr, EVENT_FLEXIBLE);
  1348. }
  1349. static int event_enable_on_exec(struct perf_event *event,
  1350. struct perf_event_context *ctx)
  1351. {
  1352. if (!event->attr.enable_on_exec)
  1353. return 0;
  1354. event->attr.enable_on_exec = 0;
  1355. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1356. return 0;
  1357. __perf_event_mark_enabled(event, ctx);
  1358. return 1;
  1359. }
  1360. /*
  1361. * Enable all of a task's events that have been marked enable-on-exec.
  1362. * This expects task == current.
  1363. */
  1364. static void perf_event_enable_on_exec(struct task_struct *task)
  1365. {
  1366. struct perf_event_context *ctx;
  1367. struct perf_event *event;
  1368. unsigned long flags;
  1369. int enabled = 0;
  1370. int ret;
  1371. local_irq_save(flags);
  1372. ctx = task->perf_event_ctxp;
  1373. if (!ctx || !ctx->nr_events)
  1374. goto out;
  1375. __perf_event_task_sched_out(ctx);
  1376. raw_spin_lock(&ctx->lock);
  1377. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1378. ret = event_enable_on_exec(event, ctx);
  1379. if (ret)
  1380. enabled = 1;
  1381. }
  1382. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1383. ret = event_enable_on_exec(event, ctx);
  1384. if (ret)
  1385. enabled = 1;
  1386. }
  1387. /*
  1388. * Unclone this context if we enabled any event.
  1389. */
  1390. if (enabled)
  1391. unclone_ctx(ctx);
  1392. raw_spin_unlock(&ctx->lock);
  1393. perf_event_task_sched_in(task);
  1394. out:
  1395. local_irq_restore(flags);
  1396. }
  1397. /*
  1398. * Cross CPU call to read the hardware event
  1399. */
  1400. static void __perf_event_read(void *info)
  1401. {
  1402. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1403. struct perf_event *event = info;
  1404. struct perf_event_context *ctx = event->ctx;
  1405. /*
  1406. * If this is a task context, we need to check whether it is
  1407. * the current task context of this cpu. If not it has been
  1408. * scheduled out before the smp call arrived. In that case
  1409. * event->count would have been updated to a recent sample
  1410. * when the event was scheduled out.
  1411. */
  1412. if (ctx->task && cpuctx->task_ctx != ctx)
  1413. return;
  1414. raw_spin_lock(&ctx->lock);
  1415. update_context_time(ctx);
  1416. update_event_times(event);
  1417. raw_spin_unlock(&ctx->lock);
  1418. event->pmu->read(event);
  1419. }
  1420. static inline u64 perf_event_count(struct perf_event *event)
  1421. {
  1422. return local64_read(&event->count) + atomic64_read(&event->child_count);
  1423. }
  1424. static u64 perf_event_read(struct perf_event *event)
  1425. {
  1426. /*
  1427. * If event is enabled and currently active on a CPU, update the
  1428. * value in the event structure:
  1429. */
  1430. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1431. smp_call_function_single(event->oncpu,
  1432. __perf_event_read, event, 1);
  1433. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1434. struct perf_event_context *ctx = event->ctx;
  1435. unsigned long flags;
  1436. raw_spin_lock_irqsave(&ctx->lock, flags);
  1437. update_context_time(ctx);
  1438. update_event_times(event);
  1439. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1440. }
  1441. return perf_event_count(event);
  1442. }
  1443. /*
  1444. * Callchain support
  1445. */
  1446. struct callchain_cpus_entries {
  1447. struct rcu_head rcu_head;
  1448. struct perf_callchain_entry *cpu_entries[0];
  1449. };
  1450. static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
  1451. static atomic_t nr_callchain_events;
  1452. static DEFINE_MUTEX(callchain_mutex);
  1453. struct callchain_cpus_entries *callchain_cpus_entries;
  1454. __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1455. struct pt_regs *regs)
  1456. {
  1457. }
  1458. __weak void perf_callchain_user(struct perf_callchain_entry *entry,
  1459. struct pt_regs *regs)
  1460. {
  1461. }
  1462. static void release_callchain_buffers_rcu(struct rcu_head *head)
  1463. {
  1464. struct callchain_cpus_entries *entries;
  1465. int cpu;
  1466. entries = container_of(head, struct callchain_cpus_entries, rcu_head);
  1467. for_each_possible_cpu(cpu)
  1468. kfree(entries->cpu_entries[cpu]);
  1469. kfree(entries);
  1470. }
  1471. static void release_callchain_buffers(void)
  1472. {
  1473. struct callchain_cpus_entries *entries;
  1474. entries = callchain_cpus_entries;
  1475. rcu_assign_pointer(callchain_cpus_entries, NULL);
  1476. call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
  1477. }
  1478. static int alloc_callchain_buffers(void)
  1479. {
  1480. int cpu;
  1481. int size;
  1482. struct callchain_cpus_entries *entries;
  1483. /*
  1484. * We can't use the percpu allocation API for data that can be
  1485. * accessed from NMI. Use a temporary manual per cpu allocation
  1486. * until that gets sorted out.
  1487. */
  1488. size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
  1489. num_possible_cpus();
  1490. entries = kzalloc(size, GFP_KERNEL);
  1491. if (!entries)
  1492. return -ENOMEM;
  1493. size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
  1494. for_each_possible_cpu(cpu) {
  1495. entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
  1496. cpu_to_node(cpu));
  1497. if (!entries->cpu_entries[cpu])
  1498. goto fail;
  1499. }
  1500. rcu_assign_pointer(callchain_cpus_entries, entries);
  1501. return 0;
  1502. fail:
  1503. for_each_possible_cpu(cpu)
  1504. kfree(entries->cpu_entries[cpu]);
  1505. kfree(entries);
  1506. return -ENOMEM;
  1507. }
  1508. static int get_callchain_buffers(void)
  1509. {
  1510. int err = 0;
  1511. int count;
  1512. mutex_lock(&callchain_mutex);
  1513. count = atomic_inc_return(&nr_callchain_events);
  1514. if (WARN_ON_ONCE(count < 1)) {
  1515. err = -EINVAL;
  1516. goto exit;
  1517. }
  1518. if (count > 1) {
  1519. /* If the allocation failed, give up */
  1520. if (!callchain_cpus_entries)
  1521. err = -ENOMEM;
  1522. goto exit;
  1523. }
  1524. err = alloc_callchain_buffers();
  1525. if (err)
  1526. release_callchain_buffers();
  1527. exit:
  1528. mutex_unlock(&callchain_mutex);
  1529. return err;
  1530. }
  1531. static void put_callchain_buffers(void)
  1532. {
  1533. if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
  1534. release_callchain_buffers();
  1535. mutex_unlock(&callchain_mutex);
  1536. }
  1537. }
  1538. static int get_recursion_context(int *recursion)
  1539. {
  1540. int rctx;
  1541. if (in_nmi())
  1542. rctx = 3;
  1543. else if (in_irq())
  1544. rctx = 2;
  1545. else if (in_softirq())
  1546. rctx = 1;
  1547. else
  1548. rctx = 0;
  1549. if (recursion[rctx])
  1550. return -1;
  1551. recursion[rctx]++;
  1552. barrier();
  1553. return rctx;
  1554. }
  1555. static inline void put_recursion_context(int *recursion, int rctx)
  1556. {
  1557. barrier();
  1558. recursion[rctx]--;
  1559. }
  1560. static struct perf_callchain_entry *get_callchain_entry(int *rctx)
  1561. {
  1562. int cpu;
  1563. struct callchain_cpus_entries *entries;
  1564. *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
  1565. if (*rctx == -1)
  1566. return NULL;
  1567. entries = rcu_dereference(callchain_cpus_entries);
  1568. if (!entries)
  1569. return NULL;
  1570. cpu = smp_processor_id();
  1571. return &entries->cpu_entries[cpu][*rctx];
  1572. }
  1573. static void
  1574. put_callchain_entry(int rctx)
  1575. {
  1576. put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
  1577. }
  1578. static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1579. {
  1580. int rctx;
  1581. struct perf_callchain_entry *entry;
  1582. entry = get_callchain_entry(&rctx);
  1583. if (rctx == -1)
  1584. return NULL;
  1585. if (!entry)
  1586. goto exit_put;
  1587. entry->nr = 0;
  1588. if (!user_mode(regs)) {
  1589. perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
  1590. perf_callchain_kernel(entry, regs);
  1591. if (current->mm)
  1592. regs = task_pt_regs(current);
  1593. else
  1594. regs = NULL;
  1595. }
  1596. if (regs) {
  1597. perf_callchain_store(entry, PERF_CONTEXT_USER);
  1598. perf_callchain_user(entry, regs);
  1599. }
  1600. exit_put:
  1601. put_callchain_entry(rctx);
  1602. return entry;
  1603. }
  1604. /*
  1605. * Initialize the perf_event context in a task_struct:
  1606. */
  1607. static void
  1608. __perf_event_init_context(struct perf_event_context *ctx,
  1609. struct task_struct *task)
  1610. {
  1611. raw_spin_lock_init(&ctx->lock);
  1612. mutex_init(&ctx->mutex);
  1613. INIT_LIST_HEAD(&ctx->pinned_groups);
  1614. INIT_LIST_HEAD(&ctx->flexible_groups);
  1615. INIT_LIST_HEAD(&ctx->event_list);
  1616. atomic_set(&ctx->refcount, 1);
  1617. ctx->task = task;
  1618. }
  1619. static struct perf_event_context *find_get_context(pid_t pid, int cpu)
  1620. {
  1621. struct perf_event_context *ctx;
  1622. struct perf_cpu_context *cpuctx;
  1623. struct task_struct *task;
  1624. unsigned long flags;
  1625. int err;
  1626. if (pid == -1 && cpu != -1) {
  1627. /* Must be root to operate on a CPU event: */
  1628. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1629. return ERR_PTR(-EACCES);
  1630. if (cpu < 0 || cpu >= nr_cpumask_bits)
  1631. return ERR_PTR(-EINVAL);
  1632. /*
  1633. * We could be clever and allow to attach a event to an
  1634. * offline CPU and activate it when the CPU comes up, but
  1635. * that's for later.
  1636. */
  1637. if (!cpu_online(cpu))
  1638. return ERR_PTR(-ENODEV);
  1639. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1640. ctx = &cpuctx->ctx;
  1641. get_ctx(ctx);
  1642. return ctx;
  1643. }
  1644. rcu_read_lock();
  1645. if (!pid)
  1646. task = current;
  1647. else
  1648. task = find_task_by_vpid(pid);
  1649. if (task)
  1650. get_task_struct(task);
  1651. rcu_read_unlock();
  1652. if (!task)
  1653. return ERR_PTR(-ESRCH);
  1654. /*
  1655. * Can't attach events to a dying task.
  1656. */
  1657. err = -ESRCH;
  1658. if (task->flags & PF_EXITING)
  1659. goto errout;
  1660. /* Reuse ptrace permission checks for now. */
  1661. err = -EACCES;
  1662. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1663. goto errout;
  1664. retry:
  1665. ctx = perf_lock_task_context(task, &flags);
  1666. if (ctx) {
  1667. unclone_ctx(ctx);
  1668. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1669. }
  1670. if (!ctx) {
  1671. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  1672. err = -ENOMEM;
  1673. if (!ctx)
  1674. goto errout;
  1675. __perf_event_init_context(ctx, task);
  1676. get_ctx(ctx);
  1677. if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
  1678. /*
  1679. * We raced with some other task; use
  1680. * the context they set.
  1681. */
  1682. kfree(ctx);
  1683. goto retry;
  1684. }
  1685. get_task_struct(task);
  1686. }
  1687. put_task_struct(task);
  1688. return ctx;
  1689. errout:
  1690. put_task_struct(task);
  1691. return ERR_PTR(err);
  1692. }
  1693. static void perf_event_free_filter(struct perf_event *event);
  1694. static void free_event_rcu(struct rcu_head *head)
  1695. {
  1696. struct perf_event *event;
  1697. event = container_of(head, struct perf_event, rcu_head);
  1698. if (event->ns)
  1699. put_pid_ns(event->ns);
  1700. perf_event_free_filter(event);
  1701. kfree(event);
  1702. }
  1703. static void perf_pending_sync(struct perf_event *event);
  1704. static void perf_buffer_put(struct perf_buffer *buffer);
  1705. static void free_event(struct perf_event *event)
  1706. {
  1707. perf_pending_sync(event);
  1708. if (!event->parent) {
  1709. atomic_dec(&nr_events);
  1710. if (event->attr.mmap || event->attr.mmap_data)
  1711. atomic_dec(&nr_mmap_events);
  1712. if (event->attr.comm)
  1713. atomic_dec(&nr_comm_events);
  1714. if (event->attr.task)
  1715. atomic_dec(&nr_task_events);
  1716. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  1717. put_callchain_buffers();
  1718. }
  1719. if (event->buffer) {
  1720. perf_buffer_put(event->buffer);
  1721. event->buffer = NULL;
  1722. }
  1723. if (event->destroy)
  1724. event->destroy(event);
  1725. put_ctx(event->ctx);
  1726. call_rcu(&event->rcu_head, free_event_rcu);
  1727. }
  1728. int perf_event_release_kernel(struct perf_event *event)
  1729. {
  1730. struct perf_event_context *ctx = event->ctx;
  1731. /*
  1732. * Remove from the PMU, can't get re-enabled since we got
  1733. * here because the last ref went.
  1734. */
  1735. perf_event_disable(event);
  1736. WARN_ON_ONCE(ctx->parent_ctx);
  1737. /*
  1738. * There are two ways this annotation is useful:
  1739. *
  1740. * 1) there is a lock recursion from perf_event_exit_task
  1741. * see the comment there.
  1742. *
  1743. * 2) there is a lock-inversion with mmap_sem through
  1744. * perf_event_read_group(), which takes faults while
  1745. * holding ctx->mutex, however this is called after
  1746. * the last filedesc died, so there is no possibility
  1747. * to trigger the AB-BA case.
  1748. */
  1749. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  1750. raw_spin_lock_irq(&ctx->lock);
  1751. perf_group_detach(event);
  1752. list_del_event(event, ctx);
  1753. raw_spin_unlock_irq(&ctx->lock);
  1754. mutex_unlock(&ctx->mutex);
  1755. mutex_lock(&event->owner->perf_event_mutex);
  1756. list_del_init(&event->owner_entry);
  1757. mutex_unlock(&event->owner->perf_event_mutex);
  1758. put_task_struct(event->owner);
  1759. free_event(event);
  1760. return 0;
  1761. }
  1762. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  1763. /*
  1764. * Called when the last reference to the file is gone.
  1765. */
  1766. static int perf_release(struct inode *inode, struct file *file)
  1767. {
  1768. struct perf_event *event = file->private_data;
  1769. file->private_data = NULL;
  1770. return perf_event_release_kernel(event);
  1771. }
  1772. static int perf_event_read_size(struct perf_event *event)
  1773. {
  1774. int entry = sizeof(u64); /* value */
  1775. int size = 0;
  1776. int nr = 1;
  1777. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1778. size += sizeof(u64);
  1779. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1780. size += sizeof(u64);
  1781. if (event->attr.read_format & PERF_FORMAT_ID)
  1782. entry += sizeof(u64);
  1783. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1784. nr += event->group_leader->nr_siblings;
  1785. size += sizeof(u64);
  1786. }
  1787. size += entry * nr;
  1788. return size;
  1789. }
  1790. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  1791. {
  1792. struct perf_event *child;
  1793. u64 total = 0;
  1794. *enabled = 0;
  1795. *running = 0;
  1796. mutex_lock(&event->child_mutex);
  1797. total += perf_event_read(event);
  1798. *enabled += event->total_time_enabled +
  1799. atomic64_read(&event->child_total_time_enabled);
  1800. *running += event->total_time_running +
  1801. atomic64_read(&event->child_total_time_running);
  1802. list_for_each_entry(child, &event->child_list, child_list) {
  1803. total += perf_event_read(child);
  1804. *enabled += child->total_time_enabled;
  1805. *running += child->total_time_running;
  1806. }
  1807. mutex_unlock(&event->child_mutex);
  1808. return total;
  1809. }
  1810. EXPORT_SYMBOL_GPL(perf_event_read_value);
  1811. static int perf_event_read_group(struct perf_event *event,
  1812. u64 read_format, char __user *buf)
  1813. {
  1814. struct perf_event *leader = event->group_leader, *sub;
  1815. int n = 0, size = 0, ret = -EFAULT;
  1816. struct perf_event_context *ctx = leader->ctx;
  1817. u64 values[5];
  1818. u64 count, enabled, running;
  1819. mutex_lock(&ctx->mutex);
  1820. count = perf_event_read_value(leader, &enabled, &running);
  1821. values[n++] = 1 + leader->nr_siblings;
  1822. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1823. values[n++] = enabled;
  1824. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1825. values[n++] = running;
  1826. values[n++] = count;
  1827. if (read_format & PERF_FORMAT_ID)
  1828. values[n++] = primary_event_id(leader);
  1829. size = n * sizeof(u64);
  1830. if (copy_to_user(buf, values, size))
  1831. goto unlock;
  1832. ret = size;
  1833. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1834. n = 0;
  1835. values[n++] = perf_event_read_value(sub, &enabled, &running);
  1836. if (read_format & PERF_FORMAT_ID)
  1837. values[n++] = primary_event_id(sub);
  1838. size = n * sizeof(u64);
  1839. if (copy_to_user(buf + ret, values, size)) {
  1840. ret = -EFAULT;
  1841. goto unlock;
  1842. }
  1843. ret += size;
  1844. }
  1845. unlock:
  1846. mutex_unlock(&ctx->mutex);
  1847. return ret;
  1848. }
  1849. static int perf_event_read_one(struct perf_event *event,
  1850. u64 read_format, char __user *buf)
  1851. {
  1852. u64 enabled, running;
  1853. u64 values[4];
  1854. int n = 0;
  1855. values[n++] = perf_event_read_value(event, &enabled, &running);
  1856. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1857. values[n++] = enabled;
  1858. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1859. values[n++] = running;
  1860. if (read_format & PERF_FORMAT_ID)
  1861. values[n++] = primary_event_id(event);
  1862. if (copy_to_user(buf, values, n * sizeof(u64)))
  1863. return -EFAULT;
  1864. return n * sizeof(u64);
  1865. }
  1866. /*
  1867. * Read the performance event - simple non blocking version for now
  1868. */
  1869. static ssize_t
  1870. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  1871. {
  1872. u64 read_format = event->attr.read_format;
  1873. int ret;
  1874. /*
  1875. * Return end-of-file for a read on a event that is in
  1876. * error state (i.e. because it was pinned but it couldn't be
  1877. * scheduled on to the CPU at some point).
  1878. */
  1879. if (event->state == PERF_EVENT_STATE_ERROR)
  1880. return 0;
  1881. if (count < perf_event_read_size(event))
  1882. return -ENOSPC;
  1883. WARN_ON_ONCE(event->ctx->parent_ctx);
  1884. if (read_format & PERF_FORMAT_GROUP)
  1885. ret = perf_event_read_group(event, read_format, buf);
  1886. else
  1887. ret = perf_event_read_one(event, read_format, buf);
  1888. return ret;
  1889. }
  1890. static ssize_t
  1891. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1892. {
  1893. struct perf_event *event = file->private_data;
  1894. return perf_read_hw(event, buf, count);
  1895. }
  1896. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1897. {
  1898. struct perf_event *event = file->private_data;
  1899. struct perf_buffer *buffer;
  1900. unsigned int events = POLL_HUP;
  1901. rcu_read_lock();
  1902. buffer = rcu_dereference(event->buffer);
  1903. if (buffer)
  1904. events = atomic_xchg(&buffer->poll, 0);
  1905. rcu_read_unlock();
  1906. poll_wait(file, &event->waitq, wait);
  1907. return events;
  1908. }
  1909. static void perf_event_reset(struct perf_event *event)
  1910. {
  1911. (void)perf_event_read(event);
  1912. local64_set(&event->count, 0);
  1913. perf_event_update_userpage(event);
  1914. }
  1915. /*
  1916. * Holding the top-level event's child_mutex means that any
  1917. * descendant process that has inherited this event will block
  1918. * in sync_child_event if it goes to exit, thus satisfying the
  1919. * task existence requirements of perf_event_enable/disable.
  1920. */
  1921. static void perf_event_for_each_child(struct perf_event *event,
  1922. void (*func)(struct perf_event *))
  1923. {
  1924. struct perf_event *child;
  1925. WARN_ON_ONCE(event->ctx->parent_ctx);
  1926. mutex_lock(&event->child_mutex);
  1927. func(event);
  1928. list_for_each_entry(child, &event->child_list, child_list)
  1929. func(child);
  1930. mutex_unlock(&event->child_mutex);
  1931. }
  1932. static void perf_event_for_each(struct perf_event *event,
  1933. void (*func)(struct perf_event *))
  1934. {
  1935. struct perf_event_context *ctx = event->ctx;
  1936. struct perf_event *sibling;
  1937. WARN_ON_ONCE(ctx->parent_ctx);
  1938. mutex_lock(&ctx->mutex);
  1939. event = event->group_leader;
  1940. perf_event_for_each_child(event, func);
  1941. func(event);
  1942. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  1943. perf_event_for_each_child(event, func);
  1944. mutex_unlock(&ctx->mutex);
  1945. }
  1946. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  1947. {
  1948. struct perf_event_context *ctx = event->ctx;
  1949. unsigned long size;
  1950. int ret = 0;
  1951. u64 value;
  1952. if (!event->attr.sample_period)
  1953. return -EINVAL;
  1954. size = copy_from_user(&value, arg, sizeof(value));
  1955. if (size != sizeof(value))
  1956. return -EFAULT;
  1957. if (!value)
  1958. return -EINVAL;
  1959. raw_spin_lock_irq(&ctx->lock);
  1960. if (event->attr.freq) {
  1961. if (value > sysctl_perf_event_sample_rate) {
  1962. ret = -EINVAL;
  1963. goto unlock;
  1964. }
  1965. event->attr.sample_freq = value;
  1966. } else {
  1967. event->attr.sample_period = value;
  1968. event->hw.sample_period = value;
  1969. }
  1970. unlock:
  1971. raw_spin_unlock_irq(&ctx->lock);
  1972. return ret;
  1973. }
  1974. static const struct file_operations perf_fops;
  1975. static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  1976. {
  1977. struct file *file;
  1978. file = fget_light(fd, fput_needed);
  1979. if (!file)
  1980. return ERR_PTR(-EBADF);
  1981. if (file->f_op != &perf_fops) {
  1982. fput_light(file, *fput_needed);
  1983. *fput_needed = 0;
  1984. return ERR_PTR(-EBADF);
  1985. }
  1986. return file->private_data;
  1987. }
  1988. static int perf_event_set_output(struct perf_event *event,
  1989. struct perf_event *output_event);
  1990. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  1991. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1992. {
  1993. struct perf_event *event = file->private_data;
  1994. void (*func)(struct perf_event *);
  1995. u32 flags = arg;
  1996. switch (cmd) {
  1997. case PERF_EVENT_IOC_ENABLE:
  1998. func = perf_event_enable;
  1999. break;
  2000. case PERF_EVENT_IOC_DISABLE:
  2001. func = perf_event_disable;
  2002. break;
  2003. case PERF_EVENT_IOC_RESET:
  2004. func = perf_event_reset;
  2005. break;
  2006. case PERF_EVENT_IOC_REFRESH:
  2007. return perf_event_refresh(event, arg);
  2008. case PERF_EVENT_IOC_PERIOD:
  2009. return perf_event_period(event, (u64 __user *)arg);
  2010. case PERF_EVENT_IOC_SET_OUTPUT:
  2011. {
  2012. struct perf_event *output_event = NULL;
  2013. int fput_needed = 0;
  2014. int ret;
  2015. if (arg != -1) {
  2016. output_event = perf_fget_light(arg, &fput_needed);
  2017. if (IS_ERR(output_event))
  2018. return PTR_ERR(output_event);
  2019. }
  2020. ret = perf_event_set_output(event, output_event);
  2021. if (output_event)
  2022. fput_light(output_event->filp, fput_needed);
  2023. return ret;
  2024. }
  2025. case PERF_EVENT_IOC_SET_FILTER:
  2026. return perf_event_set_filter(event, (void __user *)arg);
  2027. default:
  2028. return -ENOTTY;
  2029. }
  2030. if (flags & PERF_IOC_FLAG_GROUP)
  2031. perf_event_for_each(event, func);
  2032. else
  2033. perf_event_for_each_child(event, func);
  2034. return 0;
  2035. }
  2036. int perf_event_task_enable(void)
  2037. {
  2038. struct perf_event *event;
  2039. mutex_lock(&current->perf_event_mutex);
  2040. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2041. perf_event_for_each_child(event, perf_event_enable);
  2042. mutex_unlock(&current->perf_event_mutex);
  2043. return 0;
  2044. }
  2045. int perf_event_task_disable(void)
  2046. {
  2047. struct perf_event *event;
  2048. mutex_lock(&current->perf_event_mutex);
  2049. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2050. perf_event_for_each_child(event, perf_event_disable);
  2051. mutex_unlock(&current->perf_event_mutex);
  2052. return 0;
  2053. }
  2054. #ifndef PERF_EVENT_INDEX_OFFSET
  2055. # define PERF_EVENT_INDEX_OFFSET 0
  2056. #endif
  2057. static int perf_event_index(struct perf_event *event)
  2058. {
  2059. if (event->hw.state & PERF_HES_STOPPED)
  2060. return 0;
  2061. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2062. return 0;
  2063. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  2064. }
  2065. /*
  2066. * Callers need to ensure there can be no nesting of this function, otherwise
  2067. * the seqlock logic goes bad. We can not serialize this because the arch
  2068. * code calls this from NMI context.
  2069. */
  2070. void perf_event_update_userpage(struct perf_event *event)
  2071. {
  2072. struct perf_event_mmap_page *userpg;
  2073. struct perf_buffer *buffer;
  2074. rcu_read_lock();
  2075. buffer = rcu_dereference(event->buffer);
  2076. if (!buffer)
  2077. goto unlock;
  2078. userpg = buffer->user_page;
  2079. /*
  2080. * Disable preemption so as to not let the corresponding user-space
  2081. * spin too long if we get preempted.
  2082. */
  2083. preempt_disable();
  2084. ++userpg->lock;
  2085. barrier();
  2086. userpg->index = perf_event_index(event);
  2087. userpg->offset = perf_event_count(event);
  2088. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2089. userpg->offset -= local64_read(&event->hw.prev_count);
  2090. userpg->time_enabled = event->total_time_enabled +
  2091. atomic64_read(&event->child_total_time_enabled);
  2092. userpg->time_running = event->total_time_running +
  2093. atomic64_read(&event->child_total_time_running);
  2094. barrier();
  2095. ++userpg->lock;
  2096. preempt_enable();
  2097. unlock:
  2098. rcu_read_unlock();
  2099. }
  2100. static unsigned long perf_data_size(struct perf_buffer *buffer);
  2101. static void
  2102. perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
  2103. {
  2104. long max_size = perf_data_size(buffer);
  2105. if (watermark)
  2106. buffer->watermark = min(max_size, watermark);
  2107. if (!buffer->watermark)
  2108. buffer->watermark = max_size / 2;
  2109. if (flags & PERF_BUFFER_WRITABLE)
  2110. buffer->writable = 1;
  2111. atomic_set(&buffer->refcount, 1);
  2112. }
  2113. #ifndef CONFIG_PERF_USE_VMALLOC
  2114. /*
  2115. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  2116. */
  2117. static struct page *
  2118. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2119. {
  2120. if (pgoff > buffer->nr_pages)
  2121. return NULL;
  2122. if (pgoff == 0)
  2123. return virt_to_page(buffer->user_page);
  2124. return virt_to_page(buffer->data_pages[pgoff - 1]);
  2125. }
  2126. static void *perf_mmap_alloc_page(int cpu)
  2127. {
  2128. struct page *page;
  2129. int node;
  2130. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  2131. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2132. if (!page)
  2133. return NULL;
  2134. return page_address(page);
  2135. }
  2136. static struct perf_buffer *
  2137. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2138. {
  2139. struct perf_buffer *buffer;
  2140. unsigned long size;
  2141. int i;
  2142. size = sizeof(struct perf_buffer);
  2143. size += nr_pages * sizeof(void *);
  2144. buffer = kzalloc(size, GFP_KERNEL);
  2145. if (!buffer)
  2146. goto fail;
  2147. buffer->user_page = perf_mmap_alloc_page(cpu);
  2148. if (!buffer->user_page)
  2149. goto fail_user_page;
  2150. for (i = 0; i < nr_pages; i++) {
  2151. buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
  2152. if (!buffer->data_pages[i])
  2153. goto fail_data_pages;
  2154. }
  2155. buffer->nr_pages = nr_pages;
  2156. perf_buffer_init(buffer, watermark, flags);
  2157. return buffer;
  2158. fail_data_pages:
  2159. for (i--; i >= 0; i--)
  2160. free_page((unsigned long)buffer->data_pages[i]);
  2161. free_page((unsigned long)buffer->user_page);
  2162. fail_user_page:
  2163. kfree(buffer);
  2164. fail:
  2165. return NULL;
  2166. }
  2167. static void perf_mmap_free_page(unsigned long addr)
  2168. {
  2169. struct page *page = virt_to_page((void *)addr);
  2170. page->mapping = NULL;
  2171. __free_page(page);
  2172. }
  2173. static void perf_buffer_free(struct perf_buffer *buffer)
  2174. {
  2175. int i;
  2176. perf_mmap_free_page((unsigned long)buffer->user_page);
  2177. for (i = 0; i < buffer->nr_pages; i++)
  2178. perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
  2179. kfree(buffer);
  2180. }
  2181. static inline int page_order(struct perf_buffer *buffer)
  2182. {
  2183. return 0;
  2184. }
  2185. #else
  2186. /*
  2187. * Back perf_mmap() with vmalloc memory.
  2188. *
  2189. * Required for architectures that have d-cache aliasing issues.
  2190. */
  2191. static inline int page_order(struct perf_buffer *buffer)
  2192. {
  2193. return buffer->page_order;
  2194. }
  2195. static struct page *
  2196. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2197. {
  2198. if (pgoff > (1UL << page_order(buffer)))
  2199. return NULL;
  2200. return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
  2201. }
  2202. static void perf_mmap_unmark_page(void *addr)
  2203. {
  2204. struct page *page = vmalloc_to_page(addr);
  2205. page->mapping = NULL;
  2206. }
  2207. static void perf_buffer_free_work(struct work_struct *work)
  2208. {
  2209. struct perf_buffer *buffer;
  2210. void *base;
  2211. int i, nr;
  2212. buffer = container_of(work, struct perf_buffer, work);
  2213. nr = 1 << page_order(buffer);
  2214. base = buffer->user_page;
  2215. for (i = 0; i < nr + 1; i++)
  2216. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  2217. vfree(base);
  2218. kfree(buffer);
  2219. }
  2220. static void perf_buffer_free(struct perf_buffer *buffer)
  2221. {
  2222. schedule_work(&buffer->work);
  2223. }
  2224. static struct perf_buffer *
  2225. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2226. {
  2227. struct perf_buffer *buffer;
  2228. unsigned long size;
  2229. void *all_buf;
  2230. size = sizeof(struct perf_buffer);
  2231. size += sizeof(void *);
  2232. buffer = kzalloc(size, GFP_KERNEL);
  2233. if (!buffer)
  2234. goto fail;
  2235. INIT_WORK(&buffer->work, perf_buffer_free_work);
  2236. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  2237. if (!all_buf)
  2238. goto fail_all_buf;
  2239. buffer->user_page = all_buf;
  2240. buffer->data_pages[0] = all_buf + PAGE_SIZE;
  2241. buffer->page_order = ilog2(nr_pages);
  2242. buffer->nr_pages = 1;
  2243. perf_buffer_init(buffer, watermark, flags);
  2244. return buffer;
  2245. fail_all_buf:
  2246. kfree(buffer);
  2247. fail:
  2248. return NULL;
  2249. }
  2250. #endif
  2251. static unsigned long perf_data_size(struct perf_buffer *buffer)
  2252. {
  2253. return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
  2254. }
  2255. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2256. {
  2257. struct perf_event *event = vma->vm_file->private_data;
  2258. struct perf_buffer *buffer;
  2259. int ret = VM_FAULT_SIGBUS;
  2260. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2261. if (vmf->pgoff == 0)
  2262. ret = 0;
  2263. return ret;
  2264. }
  2265. rcu_read_lock();
  2266. buffer = rcu_dereference(event->buffer);
  2267. if (!buffer)
  2268. goto unlock;
  2269. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2270. goto unlock;
  2271. vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
  2272. if (!vmf->page)
  2273. goto unlock;
  2274. get_page(vmf->page);
  2275. vmf->page->mapping = vma->vm_file->f_mapping;
  2276. vmf->page->index = vmf->pgoff;
  2277. ret = 0;
  2278. unlock:
  2279. rcu_read_unlock();
  2280. return ret;
  2281. }
  2282. static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
  2283. {
  2284. struct perf_buffer *buffer;
  2285. buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
  2286. perf_buffer_free(buffer);
  2287. }
  2288. static struct perf_buffer *perf_buffer_get(struct perf_event *event)
  2289. {
  2290. struct perf_buffer *buffer;
  2291. rcu_read_lock();
  2292. buffer = rcu_dereference(event->buffer);
  2293. if (buffer) {
  2294. if (!atomic_inc_not_zero(&buffer->refcount))
  2295. buffer = NULL;
  2296. }
  2297. rcu_read_unlock();
  2298. return buffer;
  2299. }
  2300. static void perf_buffer_put(struct perf_buffer *buffer)
  2301. {
  2302. if (!atomic_dec_and_test(&buffer->refcount))
  2303. return;
  2304. call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
  2305. }
  2306. static void perf_mmap_open(struct vm_area_struct *vma)
  2307. {
  2308. struct perf_event *event = vma->vm_file->private_data;
  2309. atomic_inc(&event->mmap_count);
  2310. }
  2311. static void perf_mmap_close(struct vm_area_struct *vma)
  2312. {
  2313. struct perf_event *event = vma->vm_file->private_data;
  2314. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2315. unsigned long size = perf_data_size(event->buffer);
  2316. struct user_struct *user = event->mmap_user;
  2317. struct perf_buffer *buffer = event->buffer;
  2318. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2319. vma->vm_mm->locked_vm -= event->mmap_locked;
  2320. rcu_assign_pointer(event->buffer, NULL);
  2321. mutex_unlock(&event->mmap_mutex);
  2322. perf_buffer_put(buffer);
  2323. free_uid(user);
  2324. }
  2325. }
  2326. static const struct vm_operations_struct perf_mmap_vmops = {
  2327. .open = perf_mmap_open,
  2328. .close = perf_mmap_close,
  2329. .fault = perf_mmap_fault,
  2330. .page_mkwrite = perf_mmap_fault,
  2331. };
  2332. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2333. {
  2334. struct perf_event *event = file->private_data;
  2335. unsigned long user_locked, user_lock_limit;
  2336. struct user_struct *user = current_user();
  2337. unsigned long locked, lock_limit;
  2338. struct perf_buffer *buffer;
  2339. unsigned long vma_size;
  2340. unsigned long nr_pages;
  2341. long user_extra, extra;
  2342. int ret = 0, flags = 0;
  2343. /*
  2344. * Don't allow mmap() of inherited per-task counters. This would
  2345. * create a performance issue due to all children writing to the
  2346. * same buffer.
  2347. */
  2348. if (event->cpu == -1 && event->attr.inherit)
  2349. return -EINVAL;
  2350. if (!(vma->vm_flags & VM_SHARED))
  2351. return -EINVAL;
  2352. vma_size = vma->vm_end - vma->vm_start;
  2353. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2354. /*
  2355. * If we have buffer pages ensure they're a power-of-two number, so we
  2356. * can do bitmasks instead of modulo.
  2357. */
  2358. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2359. return -EINVAL;
  2360. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2361. return -EINVAL;
  2362. if (vma->vm_pgoff != 0)
  2363. return -EINVAL;
  2364. WARN_ON_ONCE(event->ctx->parent_ctx);
  2365. mutex_lock(&event->mmap_mutex);
  2366. if (event->buffer) {
  2367. if (event->buffer->nr_pages == nr_pages)
  2368. atomic_inc(&event->buffer->refcount);
  2369. else
  2370. ret = -EINVAL;
  2371. goto unlock;
  2372. }
  2373. user_extra = nr_pages + 1;
  2374. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2375. /*
  2376. * Increase the limit linearly with more CPUs:
  2377. */
  2378. user_lock_limit *= num_online_cpus();
  2379. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2380. extra = 0;
  2381. if (user_locked > user_lock_limit)
  2382. extra = user_locked - user_lock_limit;
  2383. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2384. lock_limit >>= PAGE_SHIFT;
  2385. locked = vma->vm_mm->locked_vm + extra;
  2386. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2387. !capable(CAP_IPC_LOCK)) {
  2388. ret = -EPERM;
  2389. goto unlock;
  2390. }
  2391. WARN_ON(event->buffer);
  2392. if (vma->vm_flags & VM_WRITE)
  2393. flags |= PERF_BUFFER_WRITABLE;
  2394. buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
  2395. event->cpu, flags);
  2396. if (!buffer) {
  2397. ret = -ENOMEM;
  2398. goto unlock;
  2399. }
  2400. rcu_assign_pointer(event->buffer, buffer);
  2401. atomic_long_add(user_extra, &user->locked_vm);
  2402. event->mmap_locked = extra;
  2403. event->mmap_user = get_current_user();
  2404. vma->vm_mm->locked_vm += event->mmap_locked;
  2405. unlock:
  2406. if (!ret)
  2407. atomic_inc(&event->mmap_count);
  2408. mutex_unlock(&event->mmap_mutex);
  2409. vma->vm_flags |= VM_RESERVED;
  2410. vma->vm_ops = &perf_mmap_vmops;
  2411. return ret;
  2412. }
  2413. static int perf_fasync(int fd, struct file *filp, int on)
  2414. {
  2415. struct inode *inode = filp->f_path.dentry->d_inode;
  2416. struct perf_event *event = filp->private_data;
  2417. int retval;
  2418. mutex_lock(&inode->i_mutex);
  2419. retval = fasync_helper(fd, filp, on, &event->fasync);
  2420. mutex_unlock(&inode->i_mutex);
  2421. if (retval < 0)
  2422. return retval;
  2423. return 0;
  2424. }
  2425. static const struct file_operations perf_fops = {
  2426. .llseek = no_llseek,
  2427. .release = perf_release,
  2428. .read = perf_read,
  2429. .poll = perf_poll,
  2430. .unlocked_ioctl = perf_ioctl,
  2431. .compat_ioctl = perf_ioctl,
  2432. .mmap = perf_mmap,
  2433. .fasync = perf_fasync,
  2434. };
  2435. /*
  2436. * Perf event wakeup
  2437. *
  2438. * If there's data, ensure we set the poll() state and publish everything
  2439. * to user-space before waking everybody up.
  2440. */
  2441. void perf_event_wakeup(struct perf_event *event)
  2442. {
  2443. wake_up_all(&event->waitq);
  2444. if (event->pending_kill) {
  2445. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2446. event->pending_kill = 0;
  2447. }
  2448. }
  2449. /*
  2450. * Pending wakeups
  2451. *
  2452. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  2453. *
  2454. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  2455. * single linked list and use cmpxchg() to add entries lockless.
  2456. */
  2457. static void perf_pending_event(struct perf_pending_entry *entry)
  2458. {
  2459. struct perf_event *event = container_of(entry,
  2460. struct perf_event, pending);
  2461. if (event->pending_disable) {
  2462. event->pending_disable = 0;
  2463. __perf_event_disable(event);
  2464. }
  2465. if (event->pending_wakeup) {
  2466. event->pending_wakeup = 0;
  2467. perf_event_wakeup(event);
  2468. }
  2469. }
  2470. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  2471. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  2472. PENDING_TAIL,
  2473. };
  2474. static void perf_pending_queue(struct perf_pending_entry *entry,
  2475. void (*func)(struct perf_pending_entry *))
  2476. {
  2477. struct perf_pending_entry **head;
  2478. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  2479. return;
  2480. entry->func = func;
  2481. head = &get_cpu_var(perf_pending_head);
  2482. do {
  2483. entry->next = *head;
  2484. } while (cmpxchg(head, entry->next, entry) != entry->next);
  2485. set_perf_event_pending();
  2486. put_cpu_var(perf_pending_head);
  2487. }
  2488. static int __perf_pending_run(void)
  2489. {
  2490. struct perf_pending_entry *list;
  2491. int nr = 0;
  2492. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  2493. while (list != PENDING_TAIL) {
  2494. void (*func)(struct perf_pending_entry *);
  2495. struct perf_pending_entry *entry = list;
  2496. list = list->next;
  2497. func = entry->func;
  2498. entry->next = NULL;
  2499. /*
  2500. * Ensure we observe the unqueue before we issue the wakeup,
  2501. * so that we won't be waiting forever.
  2502. * -- see perf_not_pending().
  2503. */
  2504. smp_wmb();
  2505. func(entry);
  2506. nr++;
  2507. }
  2508. return nr;
  2509. }
  2510. static inline int perf_not_pending(struct perf_event *event)
  2511. {
  2512. /*
  2513. * If we flush on whatever cpu we run, there is a chance we don't
  2514. * need to wait.
  2515. */
  2516. get_cpu();
  2517. __perf_pending_run();
  2518. put_cpu();
  2519. /*
  2520. * Ensure we see the proper queue state before going to sleep
  2521. * so that we do not miss the wakeup. -- see perf_pending_handle()
  2522. */
  2523. smp_rmb();
  2524. return event->pending.next == NULL;
  2525. }
  2526. static void perf_pending_sync(struct perf_event *event)
  2527. {
  2528. wait_event(event->waitq, perf_not_pending(event));
  2529. }
  2530. void perf_event_do_pending(void)
  2531. {
  2532. __perf_pending_run();
  2533. }
  2534. /*
  2535. * We assume there is only KVM supporting the callbacks.
  2536. * Later on, we might change it to a list if there is
  2537. * another virtualization implementation supporting the callbacks.
  2538. */
  2539. struct perf_guest_info_callbacks *perf_guest_cbs;
  2540. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2541. {
  2542. perf_guest_cbs = cbs;
  2543. return 0;
  2544. }
  2545. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  2546. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2547. {
  2548. perf_guest_cbs = NULL;
  2549. return 0;
  2550. }
  2551. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  2552. /*
  2553. * Output
  2554. */
  2555. static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
  2556. unsigned long offset, unsigned long head)
  2557. {
  2558. unsigned long mask;
  2559. if (!buffer->writable)
  2560. return true;
  2561. mask = perf_data_size(buffer) - 1;
  2562. offset = (offset - tail) & mask;
  2563. head = (head - tail) & mask;
  2564. if ((int)(head - offset) < 0)
  2565. return false;
  2566. return true;
  2567. }
  2568. static void perf_output_wakeup(struct perf_output_handle *handle)
  2569. {
  2570. atomic_set(&handle->buffer->poll, POLL_IN);
  2571. if (handle->nmi) {
  2572. handle->event->pending_wakeup = 1;
  2573. perf_pending_queue(&handle->event->pending,
  2574. perf_pending_event);
  2575. } else
  2576. perf_event_wakeup(handle->event);
  2577. }
  2578. /*
  2579. * We need to ensure a later event_id doesn't publish a head when a former
  2580. * event isn't done writing. However since we need to deal with NMIs we
  2581. * cannot fully serialize things.
  2582. *
  2583. * We only publish the head (and generate a wakeup) when the outer-most
  2584. * event completes.
  2585. */
  2586. static void perf_output_get_handle(struct perf_output_handle *handle)
  2587. {
  2588. struct perf_buffer *buffer = handle->buffer;
  2589. preempt_disable();
  2590. local_inc(&buffer->nest);
  2591. handle->wakeup = local_read(&buffer->wakeup);
  2592. }
  2593. static void perf_output_put_handle(struct perf_output_handle *handle)
  2594. {
  2595. struct perf_buffer *buffer = handle->buffer;
  2596. unsigned long head;
  2597. again:
  2598. head = local_read(&buffer->head);
  2599. /*
  2600. * IRQ/NMI can happen here, which means we can miss a head update.
  2601. */
  2602. if (!local_dec_and_test(&buffer->nest))
  2603. goto out;
  2604. /*
  2605. * Publish the known good head. Rely on the full barrier implied
  2606. * by atomic_dec_and_test() order the buffer->head read and this
  2607. * write.
  2608. */
  2609. buffer->user_page->data_head = head;
  2610. /*
  2611. * Now check if we missed an update, rely on the (compiler)
  2612. * barrier in atomic_dec_and_test() to re-read buffer->head.
  2613. */
  2614. if (unlikely(head != local_read(&buffer->head))) {
  2615. local_inc(&buffer->nest);
  2616. goto again;
  2617. }
  2618. if (handle->wakeup != local_read(&buffer->wakeup))
  2619. perf_output_wakeup(handle);
  2620. out:
  2621. preempt_enable();
  2622. }
  2623. __always_inline void perf_output_copy(struct perf_output_handle *handle,
  2624. const void *buf, unsigned int len)
  2625. {
  2626. do {
  2627. unsigned long size = min_t(unsigned long, handle->size, len);
  2628. memcpy(handle->addr, buf, size);
  2629. len -= size;
  2630. handle->addr += size;
  2631. buf += size;
  2632. handle->size -= size;
  2633. if (!handle->size) {
  2634. struct perf_buffer *buffer = handle->buffer;
  2635. handle->page++;
  2636. handle->page &= buffer->nr_pages - 1;
  2637. handle->addr = buffer->data_pages[handle->page];
  2638. handle->size = PAGE_SIZE << page_order(buffer);
  2639. }
  2640. } while (len);
  2641. }
  2642. int perf_output_begin(struct perf_output_handle *handle,
  2643. struct perf_event *event, unsigned int size,
  2644. int nmi, int sample)
  2645. {
  2646. struct perf_buffer *buffer;
  2647. unsigned long tail, offset, head;
  2648. int have_lost;
  2649. struct {
  2650. struct perf_event_header header;
  2651. u64 id;
  2652. u64 lost;
  2653. } lost_event;
  2654. rcu_read_lock();
  2655. /*
  2656. * For inherited events we send all the output towards the parent.
  2657. */
  2658. if (event->parent)
  2659. event = event->parent;
  2660. buffer = rcu_dereference(event->buffer);
  2661. if (!buffer)
  2662. goto out;
  2663. handle->buffer = buffer;
  2664. handle->event = event;
  2665. handle->nmi = nmi;
  2666. handle->sample = sample;
  2667. if (!buffer->nr_pages)
  2668. goto out;
  2669. have_lost = local_read(&buffer->lost);
  2670. if (have_lost)
  2671. size += sizeof(lost_event);
  2672. perf_output_get_handle(handle);
  2673. do {
  2674. /*
  2675. * Userspace could choose to issue a mb() before updating the
  2676. * tail pointer. So that all reads will be completed before the
  2677. * write is issued.
  2678. */
  2679. tail = ACCESS_ONCE(buffer->user_page->data_tail);
  2680. smp_rmb();
  2681. offset = head = local_read(&buffer->head);
  2682. head += size;
  2683. if (unlikely(!perf_output_space(buffer, tail, offset, head)))
  2684. goto fail;
  2685. } while (local_cmpxchg(&buffer->head, offset, head) != offset);
  2686. if (head - local_read(&buffer->wakeup) > buffer->watermark)
  2687. local_add(buffer->watermark, &buffer->wakeup);
  2688. handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
  2689. handle->page &= buffer->nr_pages - 1;
  2690. handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
  2691. handle->addr = buffer->data_pages[handle->page];
  2692. handle->addr += handle->size;
  2693. handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
  2694. if (have_lost) {
  2695. lost_event.header.type = PERF_RECORD_LOST;
  2696. lost_event.header.misc = 0;
  2697. lost_event.header.size = sizeof(lost_event);
  2698. lost_event.id = event->id;
  2699. lost_event.lost = local_xchg(&buffer->lost, 0);
  2700. perf_output_put(handle, lost_event);
  2701. }
  2702. return 0;
  2703. fail:
  2704. local_inc(&buffer->lost);
  2705. perf_output_put_handle(handle);
  2706. out:
  2707. rcu_read_unlock();
  2708. return -ENOSPC;
  2709. }
  2710. void perf_output_end(struct perf_output_handle *handle)
  2711. {
  2712. struct perf_event *event = handle->event;
  2713. struct perf_buffer *buffer = handle->buffer;
  2714. int wakeup_events = event->attr.wakeup_events;
  2715. if (handle->sample && wakeup_events) {
  2716. int events = local_inc_return(&buffer->events);
  2717. if (events >= wakeup_events) {
  2718. local_sub(wakeup_events, &buffer->events);
  2719. local_inc(&buffer->wakeup);
  2720. }
  2721. }
  2722. perf_output_put_handle(handle);
  2723. rcu_read_unlock();
  2724. }
  2725. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  2726. {
  2727. /*
  2728. * only top level events have the pid namespace they were created in
  2729. */
  2730. if (event->parent)
  2731. event = event->parent;
  2732. return task_tgid_nr_ns(p, event->ns);
  2733. }
  2734. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  2735. {
  2736. /*
  2737. * only top level events have the pid namespace they were created in
  2738. */
  2739. if (event->parent)
  2740. event = event->parent;
  2741. return task_pid_nr_ns(p, event->ns);
  2742. }
  2743. static void perf_output_read_one(struct perf_output_handle *handle,
  2744. struct perf_event *event)
  2745. {
  2746. u64 read_format = event->attr.read_format;
  2747. u64 values[4];
  2748. int n = 0;
  2749. values[n++] = perf_event_count(event);
  2750. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2751. values[n++] = event->total_time_enabled +
  2752. atomic64_read(&event->child_total_time_enabled);
  2753. }
  2754. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2755. values[n++] = event->total_time_running +
  2756. atomic64_read(&event->child_total_time_running);
  2757. }
  2758. if (read_format & PERF_FORMAT_ID)
  2759. values[n++] = primary_event_id(event);
  2760. perf_output_copy(handle, values, n * sizeof(u64));
  2761. }
  2762. /*
  2763. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2764. */
  2765. static void perf_output_read_group(struct perf_output_handle *handle,
  2766. struct perf_event *event)
  2767. {
  2768. struct perf_event *leader = event->group_leader, *sub;
  2769. u64 read_format = event->attr.read_format;
  2770. u64 values[5];
  2771. int n = 0;
  2772. values[n++] = 1 + leader->nr_siblings;
  2773. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2774. values[n++] = leader->total_time_enabled;
  2775. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2776. values[n++] = leader->total_time_running;
  2777. if (leader != event)
  2778. leader->pmu->read(leader);
  2779. values[n++] = perf_event_count(leader);
  2780. if (read_format & PERF_FORMAT_ID)
  2781. values[n++] = primary_event_id(leader);
  2782. perf_output_copy(handle, values, n * sizeof(u64));
  2783. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2784. n = 0;
  2785. if (sub != event)
  2786. sub->pmu->read(sub);
  2787. values[n++] = perf_event_count(sub);
  2788. if (read_format & PERF_FORMAT_ID)
  2789. values[n++] = primary_event_id(sub);
  2790. perf_output_copy(handle, values, n * sizeof(u64));
  2791. }
  2792. }
  2793. static void perf_output_read(struct perf_output_handle *handle,
  2794. struct perf_event *event)
  2795. {
  2796. if (event->attr.read_format & PERF_FORMAT_GROUP)
  2797. perf_output_read_group(handle, event);
  2798. else
  2799. perf_output_read_one(handle, event);
  2800. }
  2801. void perf_output_sample(struct perf_output_handle *handle,
  2802. struct perf_event_header *header,
  2803. struct perf_sample_data *data,
  2804. struct perf_event *event)
  2805. {
  2806. u64 sample_type = data->type;
  2807. perf_output_put(handle, *header);
  2808. if (sample_type & PERF_SAMPLE_IP)
  2809. perf_output_put(handle, data->ip);
  2810. if (sample_type & PERF_SAMPLE_TID)
  2811. perf_output_put(handle, data->tid_entry);
  2812. if (sample_type & PERF_SAMPLE_TIME)
  2813. perf_output_put(handle, data->time);
  2814. if (sample_type & PERF_SAMPLE_ADDR)
  2815. perf_output_put(handle, data->addr);
  2816. if (sample_type & PERF_SAMPLE_ID)
  2817. perf_output_put(handle, data->id);
  2818. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2819. perf_output_put(handle, data->stream_id);
  2820. if (sample_type & PERF_SAMPLE_CPU)
  2821. perf_output_put(handle, data->cpu_entry);
  2822. if (sample_type & PERF_SAMPLE_PERIOD)
  2823. perf_output_put(handle, data->period);
  2824. if (sample_type & PERF_SAMPLE_READ)
  2825. perf_output_read(handle, event);
  2826. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2827. if (data->callchain) {
  2828. int size = 1;
  2829. if (data->callchain)
  2830. size += data->callchain->nr;
  2831. size *= sizeof(u64);
  2832. perf_output_copy(handle, data->callchain, size);
  2833. } else {
  2834. u64 nr = 0;
  2835. perf_output_put(handle, nr);
  2836. }
  2837. }
  2838. if (sample_type & PERF_SAMPLE_RAW) {
  2839. if (data->raw) {
  2840. perf_output_put(handle, data->raw->size);
  2841. perf_output_copy(handle, data->raw->data,
  2842. data->raw->size);
  2843. } else {
  2844. struct {
  2845. u32 size;
  2846. u32 data;
  2847. } raw = {
  2848. .size = sizeof(u32),
  2849. .data = 0,
  2850. };
  2851. perf_output_put(handle, raw);
  2852. }
  2853. }
  2854. }
  2855. void perf_prepare_sample(struct perf_event_header *header,
  2856. struct perf_sample_data *data,
  2857. struct perf_event *event,
  2858. struct pt_regs *regs)
  2859. {
  2860. u64 sample_type = event->attr.sample_type;
  2861. data->type = sample_type;
  2862. header->type = PERF_RECORD_SAMPLE;
  2863. header->size = sizeof(*header);
  2864. header->misc = 0;
  2865. header->misc |= perf_misc_flags(regs);
  2866. if (sample_type & PERF_SAMPLE_IP) {
  2867. data->ip = perf_instruction_pointer(regs);
  2868. header->size += sizeof(data->ip);
  2869. }
  2870. if (sample_type & PERF_SAMPLE_TID) {
  2871. /* namespace issues */
  2872. data->tid_entry.pid = perf_event_pid(event, current);
  2873. data->tid_entry.tid = perf_event_tid(event, current);
  2874. header->size += sizeof(data->tid_entry);
  2875. }
  2876. if (sample_type & PERF_SAMPLE_TIME) {
  2877. data->time = perf_clock();
  2878. header->size += sizeof(data->time);
  2879. }
  2880. if (sample_type & PERF_SAMPLE_ADDR)
  2881. header->size += sizeof(data->addr);
  2882. if (sample_type & PERF_SAMPLE_ID) {
  2883. data->id = primary_event_id(event);
  2884. header->size += sizeof(data->id);
  2885. }
  2886. if (sample_type & PERF_SAMPLE_STREAM_ID) {
  2887. data->stream_id = event->id;
  2888. header->size += sizeof(data->stream_id);
  2889. }
  2890. if (sample_type & PERF_SAMPLE_CPU) {
  2891. data->cpu_entry.cpu = raw_smp_processor_id();
  2892. data->cpu_entry.reserved = 0;
  2893. header->size += sizeof(data->cpu_entry);
  2894. }
  2895. if (sample_type & PERF_SAMPLE_PERIOD)
  2896. header->size += sizeof(data->period);
  2897. if (sample_type & PERF_SAMPLE_READ)
  2898. header->size += perf_event_read_size(event);
  2899. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2900. int size = 1;
  2901. data->callchain = perf_callchain(regs);
  2902. if (data->callchain)
  2903. size += data->callchain->nr;
  2904. header->size += size * sizeof(u64);
  2905. }
  2906. if (sample_type & PERF_SAMPLE_RAW) {
  2907. int size = sizeof(u32);
  2908. if (data->raw)
  2909. size += data->raw->size;
  2910. else
  2911. size += sizeof(u32);
  2912. WARN_ON_ONCE(size & (sizeof(u64)-1));
  2913. header->size += size;
  2914. }
  2915. }
  2916. static void perf_event_output(struct perf_event *event, int nmi,
  2917. struct perf_sample_data *data,
  2918. struct pt_regs *regs)
  2919. {
  2920. struct perf_output_handle handle;
  2921. struct perf_event_header header;
  2922. /* protect the callchain buffers */
  2923. rcu_read_lock();
  2924. perf_prepare_sample(&header, data, event, regs);
  2925. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  2926. goto exit;
  2927. perf_output_sample(&handle, &header, data, event);
  2928. perf_output_end(&handle);
  2929. exit:
  2930. rcu_read_unlock();
  2931. }
  2932. /*
  2933. * read event_id
  2934. */
  2935. struct perf_read_event {
  2936. struct perf_event_header header;
  2937. u32 pid;
  2938. u32 tid;
  2939. };
  2940. static void
  2941. perf_event_read_event(struct perf_event *event,
  2942. struct task_struct *task)
  2943. {
  2944. struct perf_output_handle handle;
  2945. struct perf_read_event read_event = {
  2946. .header = {
  2947. .type = PERF_RECORD_READ,
  2948. .misc = 0,
  2949. .size = sizeof(read_event) + perf_event_read_size(event),
  2950. },
  2951. .pid = perf_event_pid(event, task),
  2952. .tid = perf_event_tid(event, task),
  2953. };
  2954. int ret;
  2955. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  2956. if (ret)
  2957. return;
  2958. perf_output_put(&handle, read_event);
  2959. perf_output_read(&handle, event);
  2960. perf_output_end(&handle);
  2961. }
  2962. /*
  2963. * task tracking -- fork/exit
  2964. *
  2965. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  2966. */
  2967. struct perf_task_event {
  2968. struct task_struct *task;
  2969. struct perf_event_context *task_ctx;
  2970. struct {
  2971. struct perf_event_header header;
  2972. u32 pid;
  2973. u32 ppid;
  2974. u32 tid;
  2975. u32 ptid;
  2976. u64 time;
  2977. } event_id;
  2978. };
  2979. static void perf_event_task_output(struct perf_event *event,
  2980. struct perf_task_event *task_event)
  2981. {
  2982. struct perf_output_handle handle;
  2983. struct task_struct *task = task_event->task;
  2984. int size, ret;
  2985. size = task_event->event_id.header.size;
  2986. ret = perf_output_begin(&handle, event, size, 0, 0);
  2987. if (ret)
  2988. return;
  2989. task_event->event_id.pid = perf_event_pid(event, task);
  2990. task_event->event_id.ppid = perf_event_pid(event, current);
  2991. task_event->event_id.tid = perf_event_tid(event, task);
  2992. task_event->event_id.ptid = perf_event_tid(event, current);
  2993. perf_output_put(&handle, task_event->event_id);
  2994. perf_output_end(&handle);
  2995. }
  2996. static int perf_event_task_match(struct perf_event *event)
  2997. {
  2998. if (event->state < PERF_EVENT_STATE_INACTIVE)
  2999. return 0;
  3000. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3001. return 0;
  3002. if (event->attr.comm || event->attr.mmap ||
  3003. event->attr.mmap_data || event->attr.task)
  3004. return 1;
  3005. return 0;
  3006. }
  3007. static void perf_event_task_ctx(struct perf_event_context *ctx,
  3008. struct perf_task_event *task_event)
  3009. {
  3010. struct perf_event *event;
  3011. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3012. if (perf_event_task_match(event))
  3013. perf_event_task_output(event, task_event);
  3014. }
  3015. }
  3016. static void perf_event_task_event(struct perf_task_event *task_event)
  3017. {
  3018. struct perf_cpu_context *cpuctx;
  3019. struct perf_event_context *ctx = task_event->task_ctx;
  3020. rcu_read_lock();
  3021. cpuctx = &get_cpu_var(perf_cpu_context);
  3022. perf_event_task_ctx(&cpuctx->ctx, task_event);
  3023. if (!ctx)
  3024. ctx = rcu_dereference(current->perf_event_ctxp);
  3025. if (ctx)
  3026. perf_event_task_ctx(ctx, task_event);
  3027. put_cpu_var(perf_cpu_context);
  3028. rcu_read_unlock();
  3029. }
  3030. static void perf_event_task(struct task_struct *task,
  3031. struct perf_event_context *task_ctx,
  3032. int new)
  3033. {
  3034. struct perf_task_event task_event;
  3035. if (!atomic_read(&nr_comm_events) &&
  3036. !atomic_read(&nr_mmap_events) &&
  3037. !atomic_read(&nr_task_events))
  3038. return;
  3039. task_event = (struct perf_task_event){
  3040. .task = task,
  3041. .task_ctx = task_ctx,
  3042. .event_id = {
  3043. .header = {
  3044. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  3045. .misc = 0,
  3046. .size = sizeof(task_event.event_id),
  3047. },
  3048. /* .pid */
  3049. /* .ppid */
  3050. /* .tid */
  3051. /* .ptid */
  3052. .time = perf_clock(),
  3053. },
  3054. };
  3055. perf_event_task_event(&task_event);
  3056. }
  3057. void perf_event_fork(struct task_struct *task)
  3058. {
  3059. perf_event_task(task, NULL, 1);
  3060. }
  3061. /*
  3062. * comm tracking
  3063. */
  3064. struct perf_comm_event {
  3065. struct task_struct *task;
  3066. char *comm;
  3067. int comm_size;
  3068. struct {
  3069. struct perf_event_header header;
  3070. u32 pid;
  3071. u32 tid;
  3072. } event_id;
  3073. };
  3074. static void perf_event_comm_output(struct perf_event *event,
  3075. struct perf_comm_event *comm_event)
  3076. {
  3077. struct perf_output_handle handle;
  3078. int size = comm_event->event_id.header.size;
  3079. int ret = perf_output_begin(&handle, event, size, 0, 0);
  3080. if (ret)
  3081. return;
  3082. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  3083. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  3084. perf_output_put(&handle, comm_event->event_id);
  3085. perf_output_copy(&handle, comm_event->comm,
  3086. comm_event->comm_size);
  3087. perf_output_end(&handle);
  3088. }
  3089. static int perf_event_comm_match(struct perf_event *event)
  3090. {
  3091. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3092. return 0;
  3093. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3094. return 0;
  3095. if (event->attr.comm)
  3096. return 1;
  3097. return 0;
  3098. }
  3099. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  3100. struct perf_comm_event *comm_event)
  3101. {
  3102. struct perf_event *event;
  3103. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3104. if (perf_event_comm_match(event))
  3105. perf_event_comm_output(event, comm_event);
  3106. }
  3107. }
  3108. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  3109. {
  3110. struct perf_cpu_context *cpuctx;
  3111. struct perf_event_context *ctx;
  3112. unsigned int size;
  3113. char comm[TASK_COMM_LEN];
  3114. memset(comm, 0, sizeof(comm));
  3115. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  3116. size = ALIGN(strlen(comm)+1, sizeof(u64));
  3117. comm_event->comm = comm;
  3118. comm_event->comm_size = size;
  3119. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  3120. rcu_read_lock();
  3121. cpuctx = &get_cpu_var(perf_cpu_context);
  3122. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  3123. ctx = rcu_dereference(current->perf_event_ctxp);
  3124. if (ctx)
  3125. perf_event_comm_ctx(ctx, comm_event);
  3126. put_cpu_var(perf_cpu_context);
  3127. rcu_read_unlock();
  3128. }
  3129. void perf_event_comm(struct task_struct *task)
  3130. {
  3131. struct perf_comm_event comm_event;
  3132. if (task->perf_event_ctxp)
  3133. perf_event_enable_on_exec(task);
  3134. if (!atomic_read(&nr_comm_events))
  3135. return;
  3136. comm_event = (struct perf_comm_event){
  3137. .task = task,
  3138. /* .comm */
  3139. /* .comm_size */
  3140. .event_id = {
  3141. .header = {
  3142. .type = PERF_RECORD_COMM,
  3143. .misc = 0,
  3144. /* .size */
  3145. },
  3146. /* .pid */
  3147. /* .tid */
  3148. },
  3149. };
  3150. perf_event_comm_event(&comm_event);
  3151. }
  3152. /*
  3153. * mmap tracking
  3154. */
  3155. struct perf_mmap_event {
  3156. struct vm_area_struct *vma;
  3157. const char *file_name;
  3158. int file_size;
  3159. struct {
  3160. struct perf_event_header header;
  3161. u32 pid;
  3162. u32 tid;
  3163. u64 start;
  3164. u64 len;
  3165. u64 pgoff;
  3166. } event_id;
  3167. };
  3168. static void perf_event_mmap_output(struct perf_event *event,
  3169. struct perf_mmap_event *mmap_event)
  3170. {
  3171. struct perf_output_handle handle;
  3172. int size = mmap_event->event_id.header.size;
  3173. int ret = perf_output_begin(&handle, event, size, 0, 0);
  3174. if (ret)
  3175. return;
  3176. mmap_event->event_id.pid = perf_event_pid(event, current);
  3177. mmap_event->event_id.tid = perf_event_tid(event, current);
  3178. perf_output_put(&handle, mmap_event->event_id);
  3179. perf_output_copy(&handle, mmap_event->file_name,
  3180. mmap_event->file_size);
  3181. perf_output_end(&handle);
  3182. }
  3183. static int perf_event_mmap_match(struct perf_event *event,
  3184. struct perf_mmap_event *mmap_event,
  3185. int executable)
  3186. {
  3187. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3188. return 0;
  3189. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3190. return 0;
  3191. if ((!executable && event->attr.mmap_data) ||
  3192. (executable && event->attr.mmap))
  3193. return 1;
  3194. return 0;
  3195. }
  3196. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3197. struct perf_mmap_event *mmap_event,
  3198. int executable)
  3199. {
  3200. struct perf_event *event;
  3201. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3202. if (perf_event_mmap_match(event, mmap_event, executable))
  3203. perf_event_mmap_output(event, mmap_event);
  3204. }
  3205. }
  3206. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3207. {
  3208. struct perf_cpu_context *cpuctx;
  3209. struct perf_event_context *ctx;
  3210. struct vm_area_struct *vma = mmap_event->vma;
  3211. struct file *file = vma->vm_file;
  3212. unsigned int size;
  3213. char tmp[16];
  3214. char *buf = NULL;
  3215. const char *name;
  3216. memset(tmp, 0, sizeof(tmp));
  3217. if (file) {
  3218. /*
  3219. * d_path works from the end of the buffer backwards, so we
  3220. * need to add enough zero bytes after the string to handle
  3221. * the 64bit alignment we do later.
  3222. */
  3223. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3224. if (!buf) {
  3225. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3226. goto got_name;
  3227. }
  3228. name = d_path(&file->f_path, buf, PATH_MAX);
  3229. if (IS_ERR(name)) {
  3230. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3231. goto got_name;
  3232. }
  3233. } else {
  3234. if (arch_vma_name(mmap_event->vma)) {
  3235. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3236. sizeof(tmp));
  3237. goto got_name;
  3238. }
  3239. if (!vma->vm_mm) {
  3240. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3241. goto got_name;
  3242. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  3243. vma->vm_end >= vma->vm_mm->brk) {
  3244. name = strncpy(tmp, "[heap]", sizeof(tmp));
  3245. goto got_name;
  3246. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  3247. vma->vm_end >= vma->vm_mm->start_stack) {
  3248. name = strncpy(tmp, "[stack]", sizeof(tmp));
  3249. goto got_name;
  3250. }
  3251. name = strncpy(tmp, "//anon", sizeof(tmp));
  3252. goto got_name;
  3253. }
  3254. got_name:
  3255. size = ALIGN(strlen(name)+1, sizeof(u64));
  3256. mmap_event->file_name = name;
  3257. mmap_event->file_size = size;
  3258. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3259. rcu_read_lock();
  3260. cpuctx = &get_cpu_var(perf_cpu_context);
  3261. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
  3262. ctx = rcu_dereference(current->perf_event_ctxp);
  3263. if (ctx)
  3264. perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
  3265. put_cpu_var(perf_cpu_context);
  3266. rcu_read_unlock();
  3267. kfree(buf);
  3268. }
  3269. void perf_event_mmap(struct vm_area_struct *vma)
  3270. {
  3271. struct perf_mmap_event mmap_event;
  3272. if (!atomic_read(&nr_mmap_events))
  3273. return;
  3274. mmap_event = (struct perf_mmap_event){
  3275. .vma = vma,
  3276. /* .file_name */
  3277. /* .file_size */
  3278. .event_id = {
  3279. .header = {
  3280. .type = PERF_RECORD_MMAP,
  3281. .misc = PERF_RECORD_MISC_USER,
  3282. /* .size */
  3283. },
  3284. /* .pid */
  3285. /* .tid */
  3286. .start = vma->vm_start,
  3287. .len = vma->vm_end - vma->vm_start,
  3288. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3289. },
  3290. };
  3291. perf_event_mmap_event(&mmap_event);
  3292. }
  3293. /*
  3294. * IRQ throttle logging
  3295. */
  3296. static void perf_log_throttle(struct perf_event *event, int enable)
  3297. {
  3298. struct perf_output_handle handle;
  3299. int ret;
  3300. struct {
  3301. struct perf_event_header header;
  3302. u64 time;
  3303. u64 id;
  3304. u64 stream_id;
  3305. } throttle_event = {
  3306. .header = {
  3307. .type = PERF_RECORD_THROTTLE,
  3308. .misc = 0,
  3309. .size = sizeof(throttle_event),
  3310. },
  3311. .time = perf_clock(),
  3312. .id = primary_event_id(event),
  3313. .stream_id = event->id,
  3314. };
  3315. if (enable)
  3316. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3317. ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
  3318. if (ret)
  3319. return;
  3320. perf_output_put(&handle, throttle_event);
  3321. perf_output_end(&handle);
  3322. }
  3323. /*
  3324. * Generic event overflow handling, sampling.
  3325. */
  3326. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3327. int throttle, struct perf_sample_data *data,
  3328. struct pt_regs *regs)
  3329. {
  3330. int events = atomic_read(&event->event_limit);
  3331. struct hw_perf_event *hwc = &event->hw;
  3332. int ret = 0;
  3333. if (!throttle) {
  3334. hwc->interrupts++;
  3335. } else {
  3336. if (hwc->interrupts != MAX_INTERRUPTS) {
  3337. hwc->interrupts++;
  3338. if (HZ * hwc->interrupts >
  3339. (u64)sysctl_perf_event_sample_rate) {
  3340. hwc->interrupts = MAX_INTERRUPTS;
  3341. perf_log_throttle(event, 0);
  3342. ret = 1;
  3343. }
  3344. } else {
  3345. /*
  3346. * Keep re-disabling events even though on the previous
  3347. * pass we disabled it - just in case we raced with a
  3348. * sched-in and the event got enabled again:
  3349. */
  3350. ret = 1;
  3351. }
  3352. }
  3353. if (event->attr.freq) {
  3354. u64 now = perf_clock();
  3355. s64 delta = now - hwc->freq_time_stamp;
  3356. hwc->freq_time_stamp = now;
  3357. if (delta > 0 && delta < 2*TICK_NSEC)
  3358. perf_adjust_period(event, delta, hwc->last_period);
  3359. }
  3360. /*
  3361. * XXX event_limit might not quite work as expected on inherited
  3362. * events
  3363. */
  3364. event->pending_kill = POLL_IN;
  3365. if (events && atomic_dec_and_test(&event->event_limit)) {
  3366. ret = 1;
  3367. event->pending_kill = POLL_HUP;
  3368. if (nmi) {
  3369. event->pending_disable = 1;
  3370. perf_pending_queue(&event->pending,
  3371. perf_pending_event);
  3372. } else
  3373. perf_event_disable(event);
  3374. }
  3375. if (event->overflow_handler)
  3376. event->overflow_handler(event, nmi, data, regs);
  3377. else
  3378. perf_event_output(event, nmi, data, regs);
  3379. return ret;
  3380. }
  3381. int perf_event_overflow(struct perf_event *event, int nmi,
  3382. struct perf_sample_data *data,
  3383. struct pt_regs *regs)
  3384. {
  3385. return __perf_event_overflow(event, nmi, 1, data, regs);
  3386. }
  3387. /*
  3388. * Generic software event infrastructure
  3389. */
  3390. /*
  3391. * We directly increment event->count and keep a second value in
  3392. * event->hw.period_left to count intervals. This period event
  3393. * is kept in the range [-sample_period, 0] so that we can use the
  3394. * sign as trigger.
  3395. */
  3396. static u64 perf_swevent_set_period(struct perf_event *event)
  3397. {
  3398. struct hw_perf_event *hwc = &event->hw;
  3399. u64 period = hwc->last_period;
  3400. u64 nr, offset;
  3401. s64 old, val;
  3402. hwc->last_period = hwc->sample_period;
  3403. again:
  3404. old = val = local64_read(&hwc->period_left);
  3405. if (val < 0)
  3406. return 0;
  3407. nr = div64_u64(period + val, period);
  3408. offset = nr * period;
  3409. val -= offset;
  3410. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  3411. goto again;
  3412. return nr;
  3413. }
  3414. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3415. int nmi, struct perf_sample_data *data,
  3416. struct pt_regs *regs)
  3417. {
  3418. struct hw_perf_event *hwc = &event->hw;
  3419. int throttle = 0;
  3420. data->period = event->hw.last_period;
  3421. if (!overflow)
  3422. overflow = perf_swevent_set_period(event);
  3423. if (hwc->interrupts == MAX_INTERRUPTS)
  3424. return;
  3425. for (; overflow; overflow--) {
  3426. if (__perf_event_overflow(event, nmi, throttle,
  3427. data, regs)) {
  3428. /*
  3429. * We inhibit the overflow from happening when
  3430. * hwc->interrupts == MAX_INTERRUPTS.
  3431. */
  3432. break;
  3433. }
  3434. throttle = 1;
  3435. }
  3436. }
  3437. static void perf_swevent_event(struct perf_event *event, u64 nr,
  3438. int nmi, struct perf_sample_data *data,
  3439. struct pt_regs *regs)
  3440. {
  3441. struct hw_perf_event *hwc = &event->hw;
  3442. local64_add(nr, &event->count);
  3443. if (!regs)
  3444. return;
  3445. if (!hwc->sample_period)
  3446. return;
  3447. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3448. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3449. if (local64_add_negative(nr, &hwc->period_left))
  3450. return;
  3451. perf_swevent_overflow(event, 0, nmi, data, regs);
  3452. }
  3453. static int perf_exclude_event(struct perf_event *event,
  3454. struct pt_regs *regs)
  3455. {
  3456. if (event->hw.state & PERF_HES_STOPPED)
  3457. return 0;
  3458. if (regs) {
  3459. if (event->attr.exclude_user && user_mode(regs))
  3460. return 1;
  3461. if (event->attr.exclude_kernel && !user_mode(regs))
  3462. return 1;
  3463. }
  3464. return 0;
  3465. }
  3466. static int perf_swevent_match(struct perf_event *event,
  3467. enum perf_type_id type,
  3468. u32 event_id,
  3469. struct perf_sample_data *data,
  3470. struct pt_regs *regs)
  3471. {
  3472. if (event->attr.type != type)
  3473. return 0;
  3474. if (event->attr.config != event_id)
  3475. return 0;
  3476. if (perf_exclude_event(event, regs))
  3477. return 0;
  3478. return 1;
  3479. }
  3480. static inline u64 swevent_hash(u64 type, u32 event_id)
  3481. {
  3482. u64 val = event_id | (type << 32);
  3483. return hash_64(val, SWEVENT_HLIST_BITS);
  3484. }
  3485. static inline struct hlist_head *
  3486. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3487. {
  3488. u64 hash = swevent_hash(type, event_id);
  3489. return &hlist->heads[hash];
  3490. }
  3491. /* For the read side: events when they trigger */
  3492. static inline struct hlist_head *
  3493. find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
  3494. {
  3495. struct swevent_hlist *hlist;
  3496. hlist = rcu_dereference(ctx->swevent_hlist);
  3497. if (!hlist)
  3498. return NULL;
  3499. return __find_swevent_head(hlist, type, event_id);
  3500. }
  3501. /* For the event head insertion and removal in the hlist */
  3502. static inline struct hlist_head *
  3503. find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
  3504. {
  3505. struct swevent_hlist *hlist;
  3506. u32 event_id = event->attr.config;
  3507. u64 type = event->attr.type;
  3508. /*
  3509. * Event scheduling is always serialized against hlist allocation
  3510. * and release. Which makes the protected version suitable here.
  3511. * The context lock guarantees that.
  3512. */
  3513. hlist = rcu_dereference_protected(ctx->swevent_hlist,
  3514. lockdep_is_held(&event->ctx->lock));
  3515. if (!hlist)
  3516. return NULL;
  3517. return __find_swevent_head(hlist, type, event_id);
  3518. }
  3519. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3520. u64 nr, int nmi,
  3521. struct perf_sample_data *data,
  3522. struct pt_regs *regs)
  3523. {
  3524. struct perf_cpu_context *cpuctx;
  3525. struct perf_event *event;
  3526. struct hlist_node *node;
  3527. struct hlist_head *head;
  3528. cpuctx = &__get_cpu_var(perf_cpu_context);
  3529. rcu_read_lock();
  3530. head = find_swevent_head_rcu(cpuctx, type, event_id);
  3531. if (!head)
  3532. goto end;
  3533. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3534. if (perf_swevent_match(event, type, event_id, data, regs))
  3535. perf_swevent_event(event, nr, nmi, data, regs);
  3536. }
  3537. end:
  3538. rcu_read_unlock();
  3539. }
  3540. int perf_swevent_get_recursion_context(void)
  3541. {
  3542. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  3543. return get_recursion_context(cpuctx->recursion);
  3544. }
  3545. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3546. void inline perf_swevent_put_recursion_context(int rctx)
  3547. {
  3548. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  3549. put_recursion_context(cpuctx->recursion, rctx);
  3550. }
  3551. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3552. struct pt_regs *regs, u64 addr)
  3553. {
  3554. struct perf_sample_data data;
  3555. int rctx;
  3556. preempt_disable_notrace();
  3557. rctx = perf_swevent_get_recursion_context();
  3558. if (rctx < 0)
  3559. return;
  3560. perf_sample_data_init(&data, addr);
  3561. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3562. perf_swevent_put_recursion_context(rctx);
  3563. preempt_enable_notrace();
  3564. }
  3565. static void perf_swevent_read(struct perf_event *event)
  3566. {
  3567. }
  3568. static int perf_swevent_add(struct perf_event *event, int flags)
  3569. {
  3570. struct hw_perf_event *hwc = &event->hw;
  3571. struct perf_cpu_context *cpuctx;
  3572. struct hlist_head *head;
  3573. cpuctx = &__get_cpu_var(perf_cpu_context);
  3574. if (hwc->sample_period) {
  3575. hwc->last_period = hwc->sample_period;
  3576. perf_swevent_set_period(event);
  3577. }
  3578. hwc->state = !(flags & PERF_EF_START);
  3579. head = find_swevent_head(cpuctx, event);
  3580. if (WARN_ON_ONCE(!head))
  3581. return -EINVAL;
  3582. hlist_add_head_rcu(&event->hlist_entry, head);
  3583. return 0;
  3584. }
  3585. static void perf_swevent_del(struct perf_event *event, int flags)
  3586. {
  3587. hlist_del_rcu(&event->hlist_entry);
  3588. }
  3589. static void perf_swevent_start(struct perf_event *event, int flags)
  3590. {
  3591. event->hw.state = 0;
  3592. }
  3593. static void perf_swevent_stop(struct perf_event *event, int flags)
  3594. {
  3595. event->hw.state = PERF_HES_STOPPED;
  3596. }
  3597. /* Deref the hlist from the update side */
  3598. static inline struct swevent_hlist *
  3599. swevent_hlist_deref(struct perf_cpu_context *cpuctx)
  3600. {
  3601. return rcu_dereference_protected(cpuctx->swevent_hlist,
  3602. lockdep_is_held(&cpuctx->hlist_mutex));
  3603. }
  3604. static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
  3605. {
  3606. struct swevent_hlist *hlist;
  3607. hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
  3608. kfree(hlist);
  3609. }
  3610. static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
  3611. {
  3612. struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
  3613. if (!hlist)
  3614. return;
  3615. rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
  3616. call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
  3617. }
  3618. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  3619. {
  3620. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  3621. mutex_lock(&cpuctx->hlist_mutex);
  3622. if (!--cpuctx->hlist_refcount)
  3623. swevent_hlist_release(cpuctx);
  3624. mutex_unlock(&cpuctx->hlist_mutex);
  3625. }
  3626. static void swevent_hlist_put(struct perf_event *event)
  3627. {
  3628. int cpu;
  3629. if (event->cpu != -1) {
  3630. swevent_hlist_put_cpu(event, event->cpu);
  3631. return;
  3632. }
  3633. for_each_possible_cpu(cpu)
  3634. swevent_hlist_put_cpu(event, cpu);
  3635. }
  3636. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  3637. {
  3638. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  3639. int err = 0;
  3640. mutex_lock(&cpuctx->hlist_mutex);
  3641. if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
  3642. struct swevent_hlist *hlist;
  3643. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  3644. if (!hlist) {
  3645. err = -ENOMEM;
  3646. goto exit;
  3647. }
  3648. rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
  3649. }
  3650. cpuctx->hlist_refcount++;
  3651. exit:
  3652. mutex_unlock(&cpuctx->hlist_mutex);
  3653. return err;
  3654. }
  3655. static int swevent_hlist_get(struct perf_event *event)
  3656. {
  3657. int err;
  3658. int cpu, failed_cpu;
  3659. if (event->cpu != -1)
  3660. return swevent_hlist_get_cpu(event, event->cpu);
  3661. get_online_cpus();
  3662. for_each_possible_cpu(cpu) {
  3663. err = swevent_hlist_get_cpu(event, cpu);
  3664. if (err) {
  3665. failed_cpu = cpu;
  3666. goto fail;
  3667. }
  3668. }
  3669. put_online_cpus();
  3670. return 0;
  3671. fail:
  3672. for_each_possible_cpu(cpu) {
  3673. if (cpu == failed_cpu)
  3674. break;
  3675. swevent_hlist_put_cpu(event, cpu);
  3676. }
  3677. put_online_cpus();
  3678. return err;
  3679. }
  3680. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3681. static void sw_perf_event_destroy(struct perf_event *event)
  3682. {
  3683. u64 event_id = event->attr.config;
  3684. WARN_ON(event->parent);
  3685. atomic_dec(&perf_swevent_enabled[event_id]);
  3686. swevent_hlist_put(event);
  3687. }
  3688. static int perf_swevent_init(struct perf_event *event)
  3689. {
  3690. int event_id = event->attr.config;
  3691. if (event->attr.type != PERF_TYPE_SOFTWARE)
  3692. return -ENOENT;
  3693. switch (event_id) {
  3694. case PERF_COUNT_SW_CPU_CLOCK:
  3695. case PERF_COUNT_SW_TASK_CLOCK:
  3696. return -ENOENT;
  3697. default:
  3698. break;
  3699. }
  3700. if (event_id > PERF_COUNT_SW_MAX)
  3701. return -ENOENT;
  3702. if (!event->parent) {
  3703. int err;
  3704. err = swevent_hlist_get(event);
  3705. if (err)
  3706. return err;
  3707. atomic_inc(&perf_swevent_enabled[event_id]);
  3708. event->destroy = sw_perf_event_destroy;
  3709. }
  3710. return 0;
  3711. }
  3712. static struct pmu perf_swevent = {
  3713. .event_init = perf_swevent_init,
  3714. .add = perf_swevent_add,
  3715. .del = perf_swevent_del,
  3716. .start = perf_swevent_start,
  3717. .stop = perf_swevent_stop,
  3718. .read = perf_swevent_read,
  3719. };
  3720. #ifdef CONFIG_EVENT_TRACING
  3721. static int perf_tp_filter_match(struct perf_event *event,
  3722. struct perf_sample_data *data)
  3723. {
  3724. void *record = data->raw->data;
  3725. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3726. return 1;
  3727. return 0;
  3728. }
  3729. static int perf_tp_event_match(struct perf_event *event,
  3730. struct perf_sample_data *data,
  3731. struct pt_regs *regs)
  3732. {
  3733. /*
  3734. * All tracepoints are from kernel-space.
  3735. */
  3736. if (event->attr.exclude_kernel)
  3737. return 0;
  3738. if (!perf_tp_filter_match(event, data))
  3739. return 0;
  3740. return 1;
  3741. }
  3742. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  3743. struct pt_regs *regs, struct hlist_head *head, int rctx)
  3744. {
  3745. struct perf_sample_data data;
  3746. struct perf_event *event;
  3747. struct hlist_node *node;
  3748. struct perf_raw_record raw = {
  3749. .size = entry_size,
  3750. .data = record,
  3751. };
  3752. perf_sample_data_init(&data, addr);
  3753. data.raw = &raw;
  3754. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3755. if (perf_tp_event_match(event, &data, regs))
  3756. perf_swevent_event(event, count, 1, &data, regs);
  3757. }
  3758. perf_swevent_put_recursion_context(rctx);
  3759. }
  3760. EXPORT_SYMBOL_GPL(perf_tp_event);
  3761. static void tp_perf_event_destroy(struct perf_event *event)
  3762. {
  3763. perf_trace_destroy(event);
  3764. }
  3765. static int perf_tp_event_init(struct perf_event *event)
  3766. {
  3767. int err;
  3768. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3769. return -ENOENT;
  3770. /*
  3771. * Raw tracepoint data is a severe data leak, only allow root to
  3772. * have these.
  3773. */
  3774. if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
  3775. perf_paranoid_tracepoint_raw() &&
  3776. !capable(CAP_SYS_ADMIN))
  3777. return -EPERM;
  3778. err = perf_trace_init(event);
  3779. if (err)
  3780. return err;
  3781. event->destroy = tp_perf_event_destroy;
  3782. return 0;
  3783. }
  3784. static struct pmu perf_tracepoint = {
  3785. .event_init = perf_tp_event_init,
  3786. .add = perf_trace_add,
  3787. .del = perf_trace_del,
  3788. .start = perf_swevent_start,
  3789. .stop = perf_swevent_stop,
  3790. .read = perf_swevent_read,
  3791. };
  3792. static inline void perf_tp_register(void)
  3793. {
  3794. perf_pmu_register(&perf_tracepoint);
  3795. }
  3796. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3797. {
  3798. char *filter_str;
  3799. int ret;
  3800. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3801. return -EINVAL;
  3802. filter_str = strndup_user(arg, PAGE_SIZE);
  3803. if (IS_ERR(filter_str))
  3804. return PTR_ERR(filter_str);
  3805. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  3806. kfree(filter_str);
  3807. return ret;
  3808. }
  3809. static void perf_event_free_filter(struct perf_event *event)
  3810. {
  3811. ftrace_profile_free_filter(event);
  3812. }
  3813. #else
  3814. static inline void perf_tp_register(void)
  3815. {
  3816. }
  3817. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3818. {
  3819. return -ENOENT;
  3820. }
  3821. static void perf_event_free_filter(struct perf_event *event)
  3822. {
  3823. }
  3824. #endif /* CONFIG_EVENT_TRACING */
  3825. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  3826. void perf_bp_event(struct perf_event *bp, void *data)
  3827. {
  3828. struct perf_sample_data sample;
  3829. struct pt_regs *regs = data;
  3830. perf_sample_data_init(&sample, bp->attr.bp_addr);
  3831. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  3832. perf_swevent_event(bp, 1, 1, &sample, regs);
  3833. }
  3834. #endif
  3835. /*
  3836. * hrtimer based swevent callback
  3837. */
  3838. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  3839. {
  3840. enum hrtimer_restart ret = HRTIMER_RESTART;
  3841. struct perf_sample_data data;
  3842. struct pt_regs *regs;
  3843. struct perf_event *event;
  3844. u64 period;
  3845. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  3846. event->pmu->read(event);
  3847. perf_sample_data_init(&data, 0);
  3848. data.period = event->hw.last_period;
  3849. regs = get_irq_regs();
  3850. if (regs && !perf_exclude_event(event, regs)) {
  3851. if (!(event->attr.exclude_idle && current->pid == 0))
  3852. if (perf_event_overflow(event, 0, &data, regs))
  3853. ret = HRTIMER_NORESTART;
  3854. }
  3855. period = max_t(u64, 10000, event->hw.sample_period);
  3856. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  3857. return ret;
  3858. }
  3859. static void perf_swevent_start_hrtimer(struct perf_event *event)
  3860. {
  3861. struct hw_perf_event *hwc = &event->hw;
  3862. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3863. hwc->hrtimer.function = perf_swevent_hrtimer;
  3864. if (hwc->sample_period) {
  3865. s64 period = local64_read(&hwc->period_left);
  3866. if (period) {
  3867. if (period < 0)
  3868. period = 10000;
  3869. local64_set(&hwc->period_left, 0);
  3870. } else {
  3871. period = max_t(u64, 10000, hwc->sample_period);
  3872. }
  3873. __hrtimer_start_range_ns(&hwc->hrtimer,
  3874. ns_to_ktime(period), 0,
  3875. HRTIMER_MODE_REL, 0);
  3876. }
  3877. }
  3878. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  3879. {
  3880. struct hw_perf_event *hwc = &event->hw;
  3881. if (hwc->sample_period) {
  3882. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  3883. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  3884. hrtimer_cancel(&hwc->hrtimer);
  3885. }
  3886. }
  3887. /*
  3888. * Software event: cpu wall time clock
  3889. */
  3890. static void cpu_clock_event_update(struct perf_event *event)
  3891. {
  3892. s64 prev;
  3893. u64 now;
  3894. now = local_clock();
  3895. prev = local64_xchg(&event->hw.prev_count, now);
  3896. local64_add(now - prev, &event->count);
  3897. }
  3898. static void cpu_clock_event_start(struct perf_event *event, int flags)
  3899. {
  3900. local64_set(&event->hw.prev_count, local_clock());
  3901. perf_swevent_start_hrtimer(event);
  3902. }
  3903. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  3904. {
  3905. perf_swevent_cancel_hrtimer(event);
  3906. cpu_clock_event_update(event);
  3907. }
  3908. static int cpu_clock_event_add(struct perf_event *event, int flags)
  3909. {
  3910. if (flags & PERF_EF_START)
  3911. cpu_clock_event_start(event, flags);
  3912. return 0;
  3913. }
  3914. static void cpu_clock_event_del(struct perf_event *event, int flags)
  3915. {
  3916. cpu_clock_event_stop(event, flags);
  3917. }
  3918. static void cpu_clock_event_read(struct perf_event *event)
  3919. {
  3920. cpu_clock_event_update(event);
  3921. }
  3922. static int cpu_clock_event_init(struct perf_event *event)
  3923. {
  3924. if (event->attr.type != PERF_TYPE_SOFTWARE)
  3925. return -ENOENT;
  3926. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  3927. return -ENOENT;
  3928. return 0;
  3929. }
  3930. static struct pmu perf_cpu_clock = {
  3931. .event_init = cpu_clock_event_init,
  3932. .add = cpu_clock_event_add,
  3933. .del = cpu_clock_event_del,
  3934. .start = cpu_clock_event_start,
  3935. .stop = cpu_clock_event_stop,
  3936. .read = cpu_clock_event_read,
  3937. };
  3938. /*
  3939. * Software event: task time clock
  3940. */
  3941. static void task_clock_event_update(struct perf_event *event, u64 now)
  3942. {
  3943. u64 prev;
  3944. s64 delta;
  3945. prev = local64_xchg(&event->hw.prev_count, now);
  3946. delta = now - prev;
  3947. local64_add(delta, &event->count);
  3948. }
  3949. static void task_clock_event_start(struct perf_event *event, int flags)
  3950. {
  3951. local64_set(&event->hw.prev_count, event->ctx->time);
  3952. perf_swevent_start_hrtimer(event);
  3953. }
  3954. static void task_clock_event_stop(struct perf_event *event, int flags)
  3955. {
  3956. perf_swevent_cancel_hrtimer(event);
  3957. task_clock_event_update(event, event->ctx->time);
  3958. }
  3959. static int task_clock_event_add(struct perf_event *event, int flags)
  3960. {
  3961. if (flags & PERF_EF_START)
  3962. task_clock_event_start(event, flags);
  3963. return 0;
  3964. }
  3965. static void task_clock_event_del(struct perf_event *event, int flags)
  3966. {
  3967. task_clock_event_stop(event, PERF_EF_UPDATE);
  3968. }
  3969. static void task_clock_event_read(struct perf_event *event)
  3970. {
  3971. u64 time;
  3972. if (!in_nmi()) {
  3973. update_context_time(event->ctx);
  3974. time = event->ctx->time;
  3975. } else {
  3976. u64 now = perf_clock();
  3977. u64 delta = now - event->ctx->timestamp;
  3978. time = event->ctx->time + delta;
  3979. }
  3980. task_clock_event_update(event, time);
  3981. }
  3982. static int task_clock_event_init(struct perf_event *event)
  3983. {
  3984. if (event->attr.type != PERF_TYPE_SOFTWARE)
  3985. return -ENOENT;
  3986. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  3987. return -ENOENT;
  3988. return 0;
  3989. }
  3990. static struct pmu perf_task_clock = {
  3991. .event_init = task_clock_event_init,
  3992. .add = task_clock_event_add,
  3993. .del = task_clock_event_del,
  3994. .start = task_clock_event_start,
  3995. .stop = task_clock_event_stop,
  3996. .read = task_clock_event_read,
  3997. };
  3998. static LIST_HEAD(pmus);
  3999. static DEFINE_MUTEX(pmus_lock);
  4000. static struct srcu_struct pmus_srcu;
  4001. static void perf_pmu_nop_void(struct pmu *pmu)
  4002. {
  4003. }
  4004. static int perf_pmu_nop_int(struct pmu *pmu)
  4005. {
  4006. return 0;
  4007. }
  4008. static void perf_pmu_start_txn(struct pmu *pmu)
  4009. {
  4010. perf_pmu_disable(pmu);
  4011. }
  4012. static int perf_pmu_commit_txn(struct pmu *pmu)
  4013. {
  4014. perf_pmu_enable(pmu);
  4015. return 0;
  4016. }
  4017. static void perf_pmu_cancel_txn(struct pmu *pmu)
  4018. {
  4019. perf_pmu_enable(pmu);
  4020. }
  4021. int perf_pmu_register(struct pmu *pmu)
  4022. {
  4023. int ret;
  4024. mutex_lock(&pmus_lock);
  4025. ret = -ENOMEM;
  4026. pmu->pmu_disable_count = alloc_percpu(int);
  4027. if (!pmu->pmu_disable_count)
  4028. goto unlock;
  4029. if (!pmu->start_txn) {
  4030. if (pmu->pmu_enable) {
  4031. /*
  4032. * If we have pmu_enable/pmu_disable calls, install
  4033. * transaction stubs that use that to try and batch
  4034. * hardware accesses.
  4035. */
  4036. pmu->start_txn = perf_pmu_start_txn;
  4037. pmu->commit_txn = perf_pmu_commit_txn;
  4038. pmu->cancel_txn = perf_pmu_cancel_txn;
  4039. } else {
  4040. pmu->start_txn = perf_pmu_nop_void;
  4041. pmu->commit_txn = perf_pmu_nop_int;
  4042. pmu->cancel_txn = perf_pmu_nop_void;
  4043. }
  4044. }
  4045. if (!pmu->pmu_enable) {
  4046. pmu->pmu_enable = perf_pmu_nop_void;
  4047. pmu->pmu_disable = perf_pmu_nop_void;
  4048. }
  4049. list_add_rcu(&pmu->entry, &pmus);
  4050. ret = 0;
  4051. unlock:
  4052. mutex_unlock(&pmus_lock);
  4053. return ret;
  4054. }
  4055. void perf_pmu_unregister(struct pmu *pmu)
  4056. {
  4057. mutex_lock(&pmus_lock);
  4058. list_del_rcu(&pmu->entry);
  4059. mutex_unlock(&pmus_lock);
  4060. synchronize_srcu(&pmus_srcu);
  4061. free_percpu(pmu->pmu_disable_count);
  4062. }
  4063. struct pmu *perf_init_event(struct perf_event *event)
  4064. {
  4065. struct pmu *pmu = NULL;
  4066. int idx;
  4067. idx = srcu_read_lock(&pmus_srcu);
  4068. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4069. int ret = pmu->event_init(event);
  4070. if (!ret)
  4071. break;
  4072. if (ret != -ENOENT) {
  4073. pmu = ERR_PTR(ret);
  4074. break;
  4075. }
  4076. }
  4077. srcu_read_unlock(&pmus_srcu, idx);
  4078. return pmu;
  4079. }
  4080. /*
  4081. * Allocate and initialize a event structure
  4082. */
  4083. static struct perf_event *
  4084. perf_event_alloc(struct perf_event_attr *attr,
  4085. int cpu,
  4086. struct perf_event_context *ctx,
  4087. struct perf_event *group_leader,
  4088. struct perf_event *parent_event,
  4089. perf_overflow_handler_t overflow_handler,
  4090. gfp_t gfpflags)
  4091. {
  4092. struct pmu *pmu;
  4093. struct perf_event *event;
  4094. struct hw_perf_event *hwc;
  4095. long err;
  4096. event = kzalloc(sizeof(*event), gfpflags);
  4097. if (!event)
  4098. return ERR_PTR(-ENOMEM);
  4099. /*
  4100. * Single events are their own group leaders, with an
  4101. * empty sibling list:
  4102. */
  4103. if (!group_leader)
  4104. group_leader = event;
  4105. mutex_init(&event->child_mutex);
  4106. INIT_LIST_HEAD(&event->child_list);
  4107. INIT_LIST_HEAD(&event->group_entry);
  4108. INIT_LIST_HEAD(&event->event_entry);
  4109. INIT_LIST_HEAD(&event->sibling_list);
  4110. init_waitqueue_head(&event->waitq);
  4111. mutex_init(&event->mmap_mutex);
  4112. event->cpu = cpu;
  4113. event->attr = *attr;
  4114. event->group_leader = group_leader;
  4115. event->pmu = NULL;
  4116. event->ctx = ctx;
  4117. event->oncpu = -1;
  4118. event->parent = parent_event;
  4119. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  4120. event->id = atomic64_inc_return(&perf_event_id);
  4121. event->state = PERF_EVENT_STATE_INACTIVE;
  4122. if (!overflow_handler && parent_event)
  4123. overflow_handler = parent_event->overflow_handler;
  4124. event->overflow_handler = overflow_handler;
  4125. if (attr->disabled)
  4126. event->state = PERF_EVENT_STATE_OFF;
  4127. pmu = NULL;
  4128. hwc = &event->hw;
  4129. hwc->sample_period = attr->sample_period;
  4130. if (attr->freq && attr->sample_freq)
  4131. hwc->sample_period = 1;
  4132. hwc->last_period = hwc->sample_period;
  4133. local64_set(&hwc->period_left, hwc->sample_period);
  4134. /*
  4135. * we currently do not support PERF_FORMAT_GROUP on inherited events
  4136. */
  4137. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  4138. goto done;
  4139. pmu = perf_init_event(event);
  4140. done:
  4141. err = 0;
  4142. if (!pmu)
  4143. err = -EINVAL;
  4144. else if (IS_ERR(pmu))
  4145. err = PTR_ERR(pmu);
  4146. if (err) {
  4147. if (event->ns)
  4148. put_pid_ns(event->ns);
  4149. kfree(event);
  4150. return ERR_PTR(err);
  4151. }
  4152. event->pmu = pmu;
  4153. if (!event->parent) {
  4154. atomic_inc(&nr_events);
  4155. if (event->attr.mmap || event->attr.mmap_data)
  4156. atomic_inc(&nr_mmap_events);
  4157. if (event->attr.comm)
  4158. atomic_inc(&nr_comm_events);
  4159. if (event->attr.task)
  4160. atomic_inc(&nr_task_events);
  4161. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  4162. err = get_callchain_buffers();
  4163. if (err) {
  4164. free_event(event);
  4165. return ERR_PTR(err);
  4166. }
  4167. }
  4168. }
  4169. return event;
  4170. }
  4171. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4172. struct perf_event_attr *attr)
  4173. {
  4174. u32 size;
  4175. int ret;
  4176. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  4177. return -EFAULT;
  4178. /*
  4179. * zero the full structure, so that a short copy will be nice.
  4180. */
  4181. memset(attr, 0, sizeof(*attr));
  4182. ret = get_user(size, &uattr->size);
  4183. if (ret)
  4184. return ret;
  4185. if (size > PAGE_SIZE) /* silly large */
  4186. goto err_size;
  4187. if (!size) /* abi compat */
  4188. size = PERF_ATTR_SIZE_VER0;
  4189. if (size < PERF_ATTR_SIZE_VER0)
  4190. goto err_size;
  4191. /*
  4192. * If we're handed a bigger struct than we know of,
  4193. * ensure all the unknown bits are 0 - i.e. new
  4194. * user-space does not rely on any kernel feature
  4195. * extensions we dont know about yet.
  4196. */
  4197. if (size > sizeof(*attr)) {
  4198. unsigned char __user *addr;
  4199. unsigned char __user *end;
  4200. unsigned char val;
  4201. addr = (void __user *)uattr + sizeof(*attr);
  4202. end = (void __user *)uattr + size;
  4203. for (; addr < end; addr++) {
  4204. ret = get_user(val, addr);
  4205. if (ret)
  4206. return ret;
  4207. if (val)
  4208. goto err_size;
  4209. }
  4210. size = sizeof(*attr);
  4211. }
  4212. ret = copy_from_user(attr, uattr, size);
  4213. if (ret)
  4214. return -EFAULT;
  4215. /*
  4216. * If the type exists, the corresponding creation will verify
  4217. * the attr->config.
  4218. */
  4219. if (attr->type >= PERF_TYPE_MAX)
  4220. return -EINVAL;
  4221. if (attr->__reserved_1)
  4222. return -EINVAL;
  4223. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4224. return -EINVAL;
  4225. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4226. return -EINVAL;
  4227. out:
  4228. return ret;
  4229. err_size:
  4230. put_user(sizeof(*attr), &uattr->size);
  4231. ret = -E2BIG;
  4232. goto out;
  4233. }
  4234. static int
  4235. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4236. {
  4237. struct perf_buffer *buffer = NULL, *old_buffer = NULL;
  4238. int ret = -EINVAL;
  4239. if (!output_event)
  4240. goto set;
  4241. /* don't allow circular references */
  4242. if (event == output_event)
  4243. goto out;
  4244. /*
  4245. * Don't allow cross-cpu buffers
  4246. */
  4247. if (output_event->cpu != event->cpu)
  4248. goto out;
  4249. /*
  4250. * If its not a per-cpu buffer, it must be the same task.
  4251. */
  4252. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4253. goto out;
  4254. set:
  4255. mutex_lock(&event->mmap_mutex);
  4256. /* Can't redirect output if we've got an active mmap() */
  4257. if (atomic_read(&event->mmap_count))
  4258. goto unlock;
  4259. if (output_event) {
  4260. /* get the buffer we want to redirect to */
  4261. buffer = perf_buffer_get(output_event);
  4262. if (!buffer)
  4263. goto unlock;
  4264. }
  4265. old_buffer = event->buffer;
  4266. rcu_assign_pointer(event->buffer, buffer);
  4267. ret = 0;
  4268. unlock:
  4269. mutex_unlock(&event->mmap_mutex);
  4270. if (old_buffer)
  4271. perf_buffer_put(old_buffer);
  4272. out:
  4273. return ret;
  4274. }
  4275. /**
  4276. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4277. *
  4278. * @attr_uptr: event_id type attributes for monitoring/sampling
  4279. * @pid: target pid
  4280. * @cpu: target cpu
  4281. * @group_fd: group leader event fd
  4282. */
  4283. SYSCALL_DEFINE5(perf_event_open,
  4284. struct perf_event_attr __user *, attr_uptr,
  4285. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4286. {
  4287. struct perf_event *event, *group_leader = NULL, *output_event = NULL;
  4288. struct perf_event_attr attr;
  4289. struct perf_event_context *ctx;
  4290. struct file *event_file = NULL;
  4291. struct file *group_file = NULL;
  4292. int event_fd;
  4293. int fput_needed = 0;
  4294. int err;
  4295. /* for future expandability... */
  4296. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  4297. return -EINVAL;
  4298. err = perf_copy_attr(attr_uptr, &attr);
  4299. if (err)
  4300. return err;
  4301. if (!attr.exclude_kernel) {
  4302. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4303. return -EACCES;
  4304. }
  4305. if (attr.freq) {
  4306. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4307. return -EINVAL;
  4308. }
  4309. event_fd = get_unused_fd_flags(O_RDWR);
  4310. if (event_fd < 0)
  4311. return event_fd;
  4312. /*
  4313. * Get the target context (task or percpu):
  4314. */
  4315. ctx = find_get_context(pid, cpu);
  4316. if (IS_ERR(ctx)) {
  4317. err = PTR_ERR(ctx);
  4318. goto err_fd;
  4319. }
  4320. if (group_fd != -1) {
  4321. group_leader = perf_fget_light(group_fd, &fput_needed);
  4322. if (IS_ERR(group_leader)) {
  4323. err = PTR_ERR(group_leader);
  4324. goto err_put_context;
  4325. }
  4326. group_file = group_leader->filp;
  4327. if (flags & PERF_FLAG_FD_OUTPUT)
  4328. output_event = group_leader;
  4329. if (flags & PERF_FLAG_FD_NO_GROUP)
  4330. group_leader = NULL;
  4331. }
  4332. /*
  4333. * Look up the group leader (we will attach this event to it):
  4334. */
  4335. if (group_leader) {
  4336. err = -EINVAL;
  4337. /*
  4338. * Do not allow a recursive hierarchy (this new sibling
  4339. * becoming part of another group-sibling):
  4340. */
  4341. if (group_leader->group_leader != group_leader)
  4342. goto err_put_context;
  4343. /*
  4344. * Do not allow to attach to a group in a different
  4345. * task or CPU context:
  4346. */
  4347. if (group_leader->ctx != ctx)
  4348. goto err_put_context;
  4349. /*
  4350. * Only a group leader can be exclusive or pinned
  4351. */
  4352. if (attr.exclusive || attr.pinned)
  4353. goto err_put_context;
  4354. }
  4355. event = perf_event_alloc(&attr, cpu, ctx, group_leader,
  4356. NULL, NULL, GFP_KERNEL);
  4357. if (IS_ERR(event)) {
  4358. err = PTR_ERR(event);
  4359. goto err_put_context;
  4360. }
  4361. if (output_event) {
  4362. err = perf_event_set_output(event, output_event);
  4363. if (err)
  4364. goto err_free_put_context;
  4365. }
  4366. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  4367. if (IS_ERR(event_file)) {
  4368. err = PTR_ERR(event_file);
  4369. goto err_free_put_context;
  4370. }
  4371. event->filp = event_file;
  4372. WARN_ON_ONCE(ctx->parent_ctx);
  4373. mutex_lock(&ctx->mutex);
  4374. perf_install_in_context(ctx, event, cpu);
  4375. ++ctx->generation;
  4376. mutex_unlock(&ctx->mutex);
  4377. event->owner = current;
  4378. get_task_struct(current);
  4379. mutex_lock(&current->perf_event_mutex);
  4380. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4381. mutex_unlock(&current->perf_event_mutex);
  4382. /*
  4383. * Drop the reference on the group_event after placing the
  4384. * new event on the sibling_list. This ensures destruction
  4385. * of the group leader will find the pointer to itself in
  4386. * perf_group_detach().
  4387. */
  4388. fput_light(group_file, fput_needed);
  4389. fd_install(event_fd, event_file);
  4390. return event_fd;
  4391. err_free_put_context:
  4392. free_event(event);
  4393. err_put_context:
  4394. fput_light(group_file, fput_needed);
  4395. put_ctx(ctx);
  4396. err_fd:
  4397. put_unused_fd(event_fd);
  4398. return err;
  4399. }
  4400. /**
  4401. * perf_event_create_kernel_counter
  4402. *
  4403. * @attr: attributes of the counter to create
  4404. * @cpu: cpu in which the counter is bound
  4405. * @pid: task to profile
  4406. */
  4407. struct perf_event *
  4408. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  4409. pid_t pid,
  4410. perf_overflow_handler_t overflow_handler)
  4411. {
  4412. struct perf_event *event;
  4413. struct perf_event_context *ctx;
  4414. int err;
  4415. /*
  4416. * Get the target context (task or percpu):
  4417. */
  4418. ctx = find_get_context(pid, cpu);
  4419. if (IS_ERR(ctx)) {
  4420. err = PTR_ERR(ctx);
  4421. goto err_exit;
  4422. }
  4423. event = perf_event_alloc(attr, cpu, ctx, NULL,
  4424. NULL, overflow_handler, GFP_KERNEL);
  4425. if (IS_ERR(event)) {
  4426. err = PTR_ERR(event);
  4427. goto err_put_context;
  4428. }
  4429. event->filp = NULL;
  4430. WARN_ON_ONCE(ctx->parent_ctx);
  4431. mutex_lock(&ctx->mutex);
  4432. perf_install_in_context(ctx, event, cpu);
  4433. ++ctx->generation;
  4434. mutex_unlock(&ctx->mutex);
  4435. event->owner = current;
  4436. get_task_struct(current);
  4437. mutex_lock(&current->perf_event_mutex);
  4438. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4439. mutex_unlock(&current->perf_event_mutex);
  4440. return event;
  4441. err_put_context:
  4442. put_ctx(ctx);
  4443. err_exit:
  4444. return ERR_PTR(err);
  4445. }
  4446. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  4447. /*
  4448. * inherit a event from parent task to child task:
  4449. */
  4450. static struct perf_event *
  4451. inherit_event(struct perf_event *parent_event,
  4452. struct task_struct *parent,
  4453. struct perf_event_context *parent_ctx,
  4454. struct task_struct *child,
  4455. struct perf_event *group_leader,
  4456. struct perf_event_context *child_ctx)
  4457. {
  4458. struct perf_event *child_event;
  4459. /*
  4460. * Instead of creating recursive hierarchies of events,
  4461. * we link inherited events back to the original parent,
  4462. * which has a filp for sure, which we use as the reference
  4463. * count:
  4464. */
  4465. if (parent_event->parent)
  4466. parent_event = parent_event->parent;
  4467. child_event = perf_event_alloc(&parent_event->attr,
  4468. parent_event->cpu, child_ctx,
  4469. group_leader, parent_event,
  4470. NULL, GFP_KERNEL);
  4471. if (IS_ERR(child_event))
  4472. return child_event;
  4473. get_ctx(child_ctx);
  4474. /*
  4475. * Make the child state follow the state of the parent event,
  4476. * not its attr.disabled bit. We hold the parent's mutex,
  4477. * so we won't race with perf_event_{en, dis}able_family.
  4478. */
  4479. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  4480. child_event->state = PERF_EVENT_STATE_INACTIVE;
  4481. else
  4482. child_event->state = PERF_EVENT_STATE_OFF;
  4483. if (parent_event->attr.freq) {
  4484. u64 sample_period = parent_event->hw.sample_period;
  4485. struct hw_perf_event *hwc = &child_event->hw;
  4486. hwc->sample_period = sample_period;
  4487. hwc->last_period = sample_period;
  4488. local64_set(&hwc->period_left, sample_period);
  4489. }
  4490. child_event->overflow_handler = parent_event->overflow_handler;
  4491. /*
  4492. * Link it up in the child's context:
  4493. */
  4494. add_event_to_ctx(child_event, child_ctx);
  4495. /*
  4496. * Get a reference to the parent filp - we will fput it
  4497. * when the child event exits. This is safe to do because
  4498. * we are in the parent and we know that the filp still
  4499. * exists and has a nonzero count:
  4500. */
  4501. atomic_long_inc(&parent_event->filp->f_count);
  4502. /*
  4503. * Link this into the parent event's child list
  4504. */
  4505. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4506. mutex_lock(&parent_event->child_mutex);
  4507. list_add_tail(&child_event->child_list, &parent_event->child_list);
  4508. mutex_unlock(&parent_event->child_mutex);
  4509. return child_event;
  4510. }
  4511. static int inherit_group(struct perf_event *parent_event,
  4512. struct task_struct *parent,
  4513. struct perf_event_context *parent_ctx,
  4514. struct task_struct *child,
  4515. struct perf_event_context *child_ctx)
  4516. {
  4517. struct perf_event *leader;
  4518. struct perf_event *sub;
  4519. struct perf_event *child_ctr;
  4520. leader = inherit_event(parent_event, parent, parent_ctx,
  4521. child, NULL, child_ctx);
  4522. if (IS_ERR(leader))
  4523. return PTR_ERR(leader);
  4524. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  4525. child_ctr = inherit_event(sub, parent, parent_ctx,
  4526. child, leader, child_ctx);
  4527. if (IS_ERR(child_ctr))
  4528. return PTR_ERR(child_ctr);
  4529. }
  4530. return 0;
  4531. }
  4532. static void sync_child_event(struct perf_event *child_event,
  4533. struct task_struct *child)
  4534. {
  4535. struct perf_event *parent_event = child_event->parent;
  4536. u64 child_val;
  4537. if (child_event->attr.inherit_stat)
  4538. perf_event_read_event(child_event, child);
  4539. child_val = perf_event_count(child_event);
  4540. /*
  4541. * Add back the child's count to the parent's count:
  4542. */
  4543. atomic64_add(child_val, &parent_event->child_count);
  4544. atomic64_add(child_event->total_time_enabled,
  4545. &parent_event->child_total_time_enabled);
  4546. atomic64_add(child_event->total_time_running,
  4547. &parent_event->child_total_time_running);
  4548. /*
  4549. * Remove this event from the parent's list
  4550. */
  4551. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4552. mutex_lock(&parent_event->child_mutex);
  4553. list_del_init(&child_event->child_list);
  4554. mutex_unlock(&parent_event->child_mutex);
  4555. /*
  4556. * Release the parent event, if this was the last
  4557. * reference to it.
  4558. */
  4559. fput(parent_event->filp);
  4560. }
  4561. static void
  4562. __perf_event_exit_task(struct perf_event *child_event,
  4563. struct perf_event_context *child_ctx,
  4564. struct task_struct *child)
  4565. {
  4566. struct perf_event *parent_event;
  4567. perf_event_remove_from_context(child_event);
  4568. parent_event = child_event->parent;
  4569. /*
  4570. * It can happen that parent exits first, and has events
  4571. * that are still around due to the child reference. These
  4572. * events need to be zapped - but otherwise linger.
  4573. */
  4574. if (parent_event) {
  4575. sync_child_event(child_event, child);
  4576. free_event(child_event);
  4577. }
  4578. }
  4579. /*
  4580. * When a child task exits, feed back event values to parent events.
  4581. */
  4582. void perf_event_exit_task(struct task_struct *child)
  4583. {
  4584. struct perf_event *child_event, *tmp;
  4585. struct perf_event_context *child_ctx;
  4586. unsigned long flags;
  4587. if (likely(!child->perf_event_ctxp)) {
  4588. perf_event_task(child, NULL, 0);
  4589. return;
  4590. }
  4591. local_irq_save(flags);
  4592. /*
  4593. * We can't reschedule here because interrupts are disabled,
  4594. * and either child is current or it is a task that can't be
  4595. * scheduled, so we are now safe from rescheduling changing
  4596. * our context.
  4597. */
  4598. child_ctx = child->perf_event_ctxp;
  4599. __perf_event_task_sched_out(child_ctx);
  4600. /*
  4601. * Take the context lock here so that if find_get_context is
  4602. * reading child->perf_event_ctxp, we wait until it has
  4603. * incremented the context's refcount before we do put_ctx below.
  4604. */
  4605. raw_spin_lock(&child_ctx->lock);
  4606. child->perf_event_ctxp = NULL;
  4607. /*
  4608. * If this context is a clone; unclone it so it can't get
  4609. * swapped to another process while we're removing all
  4610. * the events from it.
  4611. */
  4612. unclone_ctx(child_ctx);
  4613. update_context_time(child_ctx);
  4614. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4615. /*
  4616. * Report the task dead after unscheduling the events so that we
  4617. * won't get any samples after PERF_RECORD_EXIT. We can however still
  4618. * get a few PERF_RECORD_READ events.
  4619. */
  4620. perf_event_task(child, child_ctx, 0);
  4621. /*
  4622. * We can recurse on the same lock type through:
  4623. *
  4624. * __perf_event_exit_task()
  4625. * sync_child_event()
  4626. * fput(parent_event->filp)
  4627. * perf_release()
  4628. * mutex_lock(&ctx->mutex)
  4629. *
  4630. * But since its the parent context it won't be the same instance.
  4631. */
  4632. mutex_lock(&child_ctx->mutex);
  4633. again:
  4634. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  4635. group_entry)
  4636. __perf_event_exit_task(child_event, child_ctx, child);
  4637. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  4638. group_entry)
  4639. __perf_event_exit_task(child_event, child_ctx, child);
  4640. /*
  4641. * If the last event was a group event, it will have appended all
  4642. * its siblings to the list, but we obtained 'tmp' before that which
  4643. * will still point to the list head terminating the iteration.
  4644. */
  4645. if (!list_empty(&child_ctx->pinned_groups) ||
  4646. !list_empty(&child_ctx->flexible_groups))
  4647. goto again;
  4648. mutex_unlock(&child_ctx->mutex);
  4649. put_ctx(child_ctx);
  4650. }
  4651. static void perf_free_event(struct perf_event *event,
  4652. struct perf_event_context *ctx)
  4653. {
  4654. struct perf_event *parent = event->parent;
  4655. if (WARN_ON_ONCE(!parent))
  4656. return;
  4657. mutex_lock(&parent->child_mutex);
  4658. list_del_init(&event->child_list);
  4659. mutex_unlock(&parent->child_mutex);
  4660. fput(parent->filp);
  4661. perf_group_detach(event);
  4662. list_del_event(event, ctx);
  4663. free_event(event);
  4664. }
  4665. /*
  4666. * free an unexposed, unused context as created by inheritance by
  4667. * init_task below, used by fork() in case of fail.
  4668. */
  4669. void perf_event_free_task(struct task_struct *task)
  4670. {
  4671. struct perf_event_context *ctx = task->perf_event_ctxp;
  4672. struct perf_event *event, *tmp;
  4673. if (!ctx)
  4674. return;
  4675. mutex_lock(&ctx->mutex);
  4676. again:
  4677. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  4678. perf_free_event(event, ctx);
  4679. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  4680. group_entry)
  4681. perf_free_event(event, ctx);
  4682. if (!list_empty(&ctx->pinned_groups) ||
  4683. !list_empty(&ctx->flexible_groups))
  4684. goto again;
  4685. mutex_unlock(&ctx->mutex);
  4686. put_ctx(ctx);
  4687. }
  4688. static int
  4689. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  4690. struct perf_event_context *parent_ctx,
  4691. struct task_struct *child,
  4692. int *inherited_all)
  4693. {
  4694. int ret;
  4695. struct perf_event_context *child_ctx = child->perf_event_ctxp;
  4696. if (!event->attr.inherit) {
  4697. *inherited_all = 0;
  4698. return 0;
  4699. }
  4700. if (!child_ctx) {
  4701. /*
  4702. * This is executed from the parent task context, so
  4703. * inherit events that have been marked for cloning.
  4704. * First allocate and initialize a context for the
  4705. * child.
  4706. */
  4707. child_ctx = kzalloc(sizeof(struct perf_event_context),
  4708. GFP_KERNEL);
  4709. if (!child_ctx)
  4710. return -ENOMEM;
  4711. __perf_event_init_context(child_ctx, child);
  4712. child->perf_event_ctxp = child_ctx;
  4713. get_task_struct(child);
  4714. }
  4715. ret = inherit_group(event, parent, parent_ctx,
  4716. child, child_ctx);
  4717. if (ret)
  4718. *inherited_all = 0;
  4719. return ret;
  4720. }
  4721. /*
  4722. * Initialize the perf_event context in task_struct
  4723. */
  4724. int perf_event_init_task(struct task_struct *child)
  4725. {
  4726. struct perf_event_context *child_ctx, *parent_ctx;
  4727. struct perf_event_context *cloned_ctx;
  4728. struct perf_event *event;
  4729. struct task_struct *parent = current;
  4730. int inherited_all = 1;
  4731. int ret = 0;
  4732. child->perf_event_ctxp = NULL;
  4733. mutex_init(&child->perf_event_mutex);
  4734. INIT_LIST_HEAD(&child->perf_event_list);
  4735. if (likely(!parent->perf_event_ctxp))
  4736. return 0;
  4737. /*
  4738. * If the parent's context is a clone, pin it so it won't get
  4739. * swapped under us.
  4740. */
  4741. parent_ctx = perf_pin_task_context(parent);
  4742. /*
  4743. * No need to check if parent_ctx != NULL here; since we saw
  4744. * it non-NULL earlier, the only reason for it to become NULL
  4745. * is if we exit, and since we're currently in the middle of
  4746. * a fork we can't be exiting at the same time.
  4747. */
  4748. /*
  4749. * Lock the parent list. No need to lock the child - not PID
  4750. * hashed yet and not running, so nobody can access it.
  4751. */
  4752. mutex_lock(&parent_ctx->mutex);
  4753. /*
  4754. * We dont have to disable NMIs - we are only looking at
  4755. * the list, not manipulating it:
  4756. */
  4757. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  4758. ret = inherit_task_group(event, parent, parent_ctx, child,
  4759. &inherited_all);
  4760. if (ret)
  4761. break;
  4762. }
  4763. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  4764. ret = inherit_task_group(event, parent, parent_ctx, child,
  4765. &inherited_all);
  4766. if (ret)
  4767. break;
  4768. }
  4769. child_ctx = child->perf_event_ctxp;
  4770. if (child_ctx && inherited_all) {
  4771. /*
  4772. * Mark the child context as a clone of the parent
  4773. * context, or of whatever the parent is a clone of.
  4774. * Note that if the parent is a clone, it could get
  4775. * uncloned at any point, but that doesn't matter
  4776. * because the list of events and the generation
  4777. * count can't have changed since we took the mutex.
  4778. */
  4779. cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
  4780. if (cloned_ctx) {
  4781. child_ctx->parent_ctx = cloned_ctx;
  4782. child_ctx->parent_gen = parent_ctx->parent_gen;
  4783. } else {
  4784. child_ctx->parent_ctx = parent_ctx;
  4785. child_ctx->parent_gen = parent_ctx->generation;
  4786. }
  4787. get_ctx(child_ctx->parent_ctx);
  4788. }
  4789. mutex_unlock(&parent_ctx->mutex);
  4790. perf_unpin_context(parent_ctx);
  4791. return ret;
  4792. }
  4793. static void __init perf_event_init_all_cpus(void)
  4794. {
  4795. int cpu;
  4796. struct perf_cpu_context *cpuctx;
  4797. for_each_possible_cpu(cpu) {
  4798. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4799. mutex_init(&cpuctx->hlist_mutex);
  4800. __perf_event_init_context(&cpuctx->ctx, NULL);
  4801. }
  4802. }
  4803. static void __cpuinit perf_event_init_cpu(int cpu)
  4804. {
  4805. struct perf_cpu_context *cpuctx;
  4806. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4807. spin_lock(&perf_resource_lock);
  4808. cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
  4809. spin_unlock(&perf_resource_lock);
  4810. mutex_lock(&cpuctx->hlist_mutex);
  4811. if (cpuctx->hlist_refcount > 0) {
  4812. struct swevent_hlist *hlist;
  4813. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  4814. WARN_ON_ONCE(!hlist);
  4815. rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
  4816. }
  4817. mutex_unlock(&cpuctx->hlist_mutex);
  4818. }
  4819. #ifdef CONFIG_HOTPLUG_CPU
  4820. static void __perf_event_exit_cpu(void *info)
  4821. {
  4822. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  4823. struct perf_event_context *ctx = &cpuctx->ctx;
  4824. struct perf_event *event, *tmp;
  4825. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  4826. __perf_event_remove_from_context(event);
  4827. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  4828. __perf_event_remove_from_context(event);
  4829. }
  4830. static void perf_event_exit_cpu(int cpu)
  4831. {
  4832. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  4833. struct perf_event_context *ctx = &cpuctx->ctx;
  4834. mutex_lock(&cpuctx->hlist_mutex);
  4835. swevent_hlist_release(cpuctx);
  4836. mutex_unlock(&cpuctx->hlist_mutex);
  4837. mutex_lock(&ctx->mutex);
  4838. smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
  4839. mutex_unlock(&ctx->mutex);
  4840. }
  4841. #else
  4842. static inline void perf_event_exit_cpu(int cpu) { }
  4843. #endif
  4844. static int __cpuinit
  4845. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  4846. {
  4847. unsigned int cpu = (long)hcpu;
  4848. switch (action & ~CPU_TASKS_FROZEN) {
  4849. case CPU_UP_PREPARE:
  4850. case CPU_DOWN_FAILED:
  4851. perf_event_init_cpu(cpu);
  4852. break;
  4853. case CPU_UP_CANCELED:
  4854. case CPU_DOWN_PREPARE:
  4855. perf_event_exit_cpu(cpu);
  4856. break;
  4857. default:
  4858. break;
  4859. }
  4860. return NOTIFY_OK;
  4861. }
  4862. void __init perf_event_init(void)
  4863. {
  4864. perf_event_init_all_cpus();
  4865. init_srcu_struct(&pmus_srcu);
  4866. perf_pmu_register(&perf_swevent);
  4867. perf_pmu_register(&perf_cpu_clock);
  4868. perf_pmu_register(&perf_task_clock);
  4869. perf_tp_register();
  4870. perf_cpu_notifier(perf_cpu_notify);
  4871. }
  4872. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
  4873. struct sysdev_class_attribute *attr,
  4874. char *buf)
  4875. {
  4876. return sprintf(buf, "%d\n", perf_reserved_percpu);
  4877. }
  4878. static ssize_t
  4879. perf_set_reserve_percpu(struct sysdev_class *class,
  4880. struct sysdev_class_attribute *attr,
  4881. const char *buf,
  4882. size_t count)
  4883. {
  4884. struct perf_cpu_context *cpuctx;
  4885. unsigned long val;
  4886. int err, cpu, mpt;
  4887. err = strict_strtoul(buf, 10, &val);
  4888. if (err)
  4889. return err;
  4890. if (val > perf_max_events)
  4891. return -EINVAL;
  4892. spin_lock(&perf_resource_lock);
  4893. perf_reserved_percpu = val;
  4894. for_each_online_cpu(cpu) {
  4895. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4896. raw_spin_lock_irq(&cpuctx->ctx.lock);
  4897. mpt = min(perf_max_events - cpuctx->ctx.nr_events,
  4898. perf_max_events - perf_reserved_percpu);
  4899. cpuctx->max_pertask = mpt;
  4900. raw_spin_unlock_irq(&cpuctx->ctx.lock);
  4901. }
  4902. spin_unlock(&perf_resource_lock);
  4903. return count;
  4904. }
  4905. static ssize_t perf_show_overcommit(struct sysdev_class *class,
  4906. struct sysdev_class_attribute *attr,
  4907. char *buf)
  4908. {
  4909. return sprintf(buf, "%d\n", perf_overcommit);
  4910. }
  4911. static ssize_t
  4912. perf_set_overcommit(struct sysdev_class *class,
  4913. struct sysdev_class_attribute *attr,
  4914. const char *buf, size_t count)
  4915. {
  4916. unsigned long val;
  4917. int err;
  4918. err = strict_strtoul(buf, 10, &val);
  4919. if (err)
  4920. return err;
  4921. if (val > 1)
  4922. return -EINVAL;
  4923. spin_lock(&perf_resource_lock);
  4924. perf_overcommit = val;
  4925. spin_unlock(&perf_resource_lock);
  4926. return count;
  4927. }
  4928. static SYSDEV_CLASS_ATTR(
  4929. reserve_percpu,
  4930. 0644,
  4931. perf_show_reserve_percpu,
  4932. perf_set_reserve_percpu
  4933. );
  4934. static SYSDEV_CLASS_ATTR(
  4935. overcommit,
  4936. 0644,
  4937. perf_show_overcommit,
  4938. perf_set_overcommit
  4939. );
  4940. static struct attribute *perfclass_attrs[] = {
  4941. &attr_reserve_percpu.attr,
  4942. &attr_overcommit.attr,
  4943. NULL
  4944. };
  4945. static struct attribute_group perfclass_attr_group = {
  4946. .attrs = perfclass_attrs,
  4947. .name = "perf_events",
  4948. };
  4949. static int __init perf_event_sysfs_init(void)
  4950. {
  4951. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  4952. &perfclass_attr_group);
  4953. }
  4954. device_initcall(perf_event_sysfs_init);