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