perf_event.c 116 KB

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