perf_event.c 122 KB

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