perf_counter.c 80 KB

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