perf_counter.c 82 KB

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