perf_counter.c 116 KB

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