perf_counter.c 78 KB

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