perf_counter.c 113 KB

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