perf_counter.c 110 KB

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