perf_event.c 118 KB

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