perf_event.c 121 KB

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