perf_counter.c 81 KB

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