perf_counter.c 117 KB

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