perf_counter.c 81 KB

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