perf_counter.c 87 KB

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