perf_counter.c 92 KB

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