perf_counter.c 79 KB

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