perf_counter.c 76 KB

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