perf_counter.c 110 KB

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