perf_counter.c 113 KB

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