perf_counter.c 117 KB

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