perf_event.c 120 KB

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