perf_counter.c 109 KB

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