perf_event.c 138 KB

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