perf_event.c 129 KB

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