perf_event.c 126 KB

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