perf_event.c 138 KB

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