perf_counter.c 99 KB

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