perf_counter.c 107 KB

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