perf_counter.c 74 KB

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