perf_counter.c 80 KB

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