perf_event.c 134 KB

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