perf_event.c 138 KB

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