perf_counter.c 80 KB

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