perf_counter.c 100 KB

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