perf_event.c 113 KB

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