perf_counter.c 93 KB

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