perf_counter.c 93 KB

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