perf_event.c 126 KB

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