perf_counter.c 108 KB

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