perf_counter.c 80 KB

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