perf_counter.c 95 KB

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