perf_event.c 121 KB

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