perf_counter.c 101 KB

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