perf_event.c 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494
  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 void perf_adjust_period(struct perf_event *event, u64 events)
  1210. {
  1211. struct hw_perf_event *hwc = &event->hw;
  1212. u64 period, sample_period;
  1213. s64 delta;
  1214. events *= hwc->sample_period;
  1215. period = div64_u64(events, event->attr.sample_freq);
  1216. delta = (s64)(period - hwc->sample_period);
  1217. delta = (delta + 7) / 8; /* low pass filter */
  1218. sample_period = hwc->sample_period + delta;
  1219. if (!sample_period)
  1220. sample_period = 1;
  1221. hwc->sample_period = sample_period;
  1222. }
  1223. static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
  1224. {
  1225. struct perf_event *event;
  1226. struct hw_perf_event *hwc;
  1227. u64 interrupts, freq;
  1228. raw_spin_lock(&ctx->lock);
  1229. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  1230. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1231. continue;
  1232. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1233. continue;
  1234. hwc = &event->hw;
  1235. interrupts = hwc->interrupts;
  1236. hwc->interrupts = 0;
  1237. /*
  1238. * unthrottle events on the tick
  1239. */
  1240. if (interrupts == MAX_INTERRUPTS) {
  1241. perf_log_throttle(event, 1);
  1242. event->pmu->unthrottle(event);
  1243. interrupts = 2*sysctl_perf_event_sample_rate/HZ;
  1244. }
  1245. if (!event->attr.freq || !event->attr.sample_freq)
  1246. continue;
  1247. /*
  1248. * if the specified freq < HZ then we need to skip ticks
  1249. */
  1250. if (event->attr.sample_freq < HZ) {
  1251. freq = event->attr.sample_freq;
  1252. hwc->freq_count += freq;
  1253. hwc->freq_interrupts += interrupts;
  1254. if (hwc->freq_count < HZ)
  1255. continue;
  1256. interrupts = hwc->freq_interrupts;
  1257. hwc->freq_interrupts = 0;
  1258. hwc->freq_count -= HZ;
  1259. } else
  1260. freq = HZ;
  1261. perf_adjust_period(event, freq * interrupts);
  1262. /*
  1263. * In order to avoid being stalled by an (accidental) huge
  1264. * sample period, force reset the sample period if we didn't
  1265. * get any events in this freq period.
  1266. */
  1267. if (!interrupts) {
  1268. perf_disable();
  1269. event->pmu->disable(event);
  1270. atomic64_set(&hwc->period_left, 0);
  1271. event->pmu->enable(event);
  1272. perf_enable();
  1273. }
  1274. }
  1275. raw_spin_unlock(&ctx->lock);
  1276. }
  1277. /*
  1278. * Round-robin a context's events:
  1279. */
  1280. static void rotate_ctx(struct perf_event_context *ctx)
  1281. {
  1282. if (!ctx->nr_events)
  1283. return;
  1284. raw_spin_lock(&ctx->lock);
  1285. /* Rotate the first entry last of non-pinned groups */
  1286. perf_disable();
  1287. list_rotate_left(&ctx->flexible_groups);
  1288. perf_enable();
  1289. raw_spin_unlock(&ctx->lock);
  1290. }
  1291. void perf_event_task_tick(struct task_struct *curr)
  1292. {
  1293. struct perf_cpu_context *cpuctx;
  1294. struct perf_event_context *ctx;
  1295. if (!atomic_read(&nr_events))
  1296. return;
  1297. cpuctx = &__get_cpu_var(perf_cpu_context);
  1298. ctx = curr->perf_event_ctxp;
  1299. perf_ctx_adjust_freq(&cpuctx->ctx);
  1300. if (ctx)
  1301. perf_ctx_adjust_freq(ctx);
  1302. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1303. if (ctx)
  1304. task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
  1305. rotate_ctx(&cpuctx->ctx);
  1306. if (ctx)
  1307. rotate_ctx(ctx);
  1308. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1309. if (ctx)
  1310. task_ctx_sched_in(curr, EVENT_FLEXIBLE);
  1311. }
  1312. static int event_enable_on_exec(struct perf_event *event,
  1313. struct perf_event_context *ctx)
  1314. {
  1315. if (!event->attr.enable_on_exec)
  1316. return 0;
  1317. event->attr.enable_on_exec = 0;
  1318. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1319. return 0;
  1320. __perf_event_mark_enabled(event, ctx);
  1321. return 1;
  1322. }
  1323. /*
  1324. * Enable all of a task's events that have been marked enable-on-exec.
  1325. * This expects task == current.
  1326. */
  1327. static void perf_event_enable_on_exec(struct task_struct *task)
  1328. {
  1329. struct perf_event_context *ctx;
  1330. struct perf_event *event;
  1331. unsigned long flags;
  1332. int enabled = 0;
  1333. int ret;
  1334. local_irq_save(flags);
  1335. ctx = task->perf_event_ctxp;
  1336. if (!ctx || !ctx->nr_events)
  1337. goto out;
  1338. __perf_event_task_sched_out(ctx);
  1339. raw_spin_lock(&ctx->lock);
  1340. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1341. ret = event_enable_on_exec(event, ctx);
  1342. if (ret)
  1343. enabled = 1;
  1344. }
  1345. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1346. ret = event_enable_on_exec(event, ctx);
  1347. if (ret)
  1348. enabled = 1;
  1349. }
  1350. /*
  1351. * Unclone this context if we enabled any event.
  1352. */
  1353. if (enabled)
  1354. unclone_ctx(ctx);
  1355. raw_spin_unlock(&ctx->lock);
  1356. perf_event_task_sched_in(task);
  1357. out:
  1358. local_irq_restore(flags);
  1359. }
  1360. /*
  1361. * Cross CPU call to read the hardware event
  1362. */
  1363. static void __perf_event_read(void *info)
  1364. {
  1365. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1366. struct perf_event *event = info;
  1367. struct perf_event_context *ctx = event->ctx;
  1368. /*
  1369. * If this is a task context, we need to check whether it is
  1370. * the current task context of this cpu. If not it has been
  1371. * scheduled out before the smp call arrived. In that case
  1372. * event->count would have been updated to a recent sample
  1373. * when the event was scheduled out.
  1374. */
  1375. if (ctx->task && cpuctx->task_ctx != ctx)
  1376. return;
  1377. raw_spin_lock(&ctx->lock);
  1378. update_context_time(ctx);
  1379. update_event_times(event);
  1380. raw_spin_unlock(&ctx->lock);
  1381. event->pmu->read(event);
  1382. }
  1383. static u64 perf_event_read(struct perf_event *event)
  1384. {
  1385. /*
  1386. * If event is enabled and currently active on a CPU, update the
  1387. * value in the event structure:
  1388. */
  1389. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1390. smp_call_function_single(event->oncpu,
  1391. __perf_event_read, event, 1);
  1392. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1393. struct perf_event_context *ctx = event->ctx;
  1394. unsigned long flags;
  1395. raw_spin_lock_irqsave(&ctx->lock, flags);
  1396. update_context_time(ctx);
  1397. update_event_times(event);
  1398. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1399. }
  1400. return atomic64_read(&event->count);
  1401. }
  1402. /*
  1403. * Initialize the perf_event context in a task_struct:
  1404. */
  1405. static void
  1406. __perf_event_init_context(struct perf_event_context *ctx,
  1407. struct task_struct *task)
  1408. {
  1409. raw_spin_lock_init(&ctx->lock);
  1410. mutex_init(&ctx->mutex);
  1411. INIT_LIST_HEAD(&ctx->pinned_groups);
  1412. INIT_LIST_HEAD(&ctx->flexible_groups);
  1413. INIT_LIST_HEAD(&ctx->event_list);
  1414. atomic_set(&ctx->refcount, 1);
  1415. ctx->task = task;
  1416. }
  1417. static struct perf_event_context *find_get_context(pid_t pid, int cpu)
  1418. {
  1419. struct perf_event_context *ctx;
  1420. struct perf_cpu_context *cpuctx;
  1421. struct task_struct *task;
  1422. unsigned long flags;
  1423. int err;
  1424. if (pid == -1 && cpu != -1) {
  1425. /* Must be root to operate on a CPU event: */
  1426. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1427. return ERR_PTR(-EACCES);
  1428. if (cpu < 0 || cpu >= nr_cpumask_bits)
  1429. return ERR_PTR(-EINVAL);
  1430. /*
  1431. * We could be clever and allow to attach a event to an
  1432. * offline CPU and activate it when the CPU comes up, but
  1433. * that's for later.
  1434. */
  1435. if (!cpu_online(cpu))
  1436. return ERR_PTR(-ENODEV);
  1437. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1438. ctx = &cpuctx->ctx;
  1439. get_ctx(ctx);
  1440. return ctx;
  1441. }
  1442. rcu_read_lock();
  1443. if (!pid)
  1444. task = current;
  1445. else
  1446. task = find_task_by_vpid(pid);
  1447. if (task)
  1448. get_task_struct(task);
  1449. rcu_read_unlock();
  1450. if (!task)
  1451. return ERR_PTR(-ESRCH);
  1452. /*
  1453. * Can't attach events to a dying task.
  1454. */
  1455. err = -ESRCH;
  1456. if (task->flags & PF_EXITING)
  1457. goto errout;
  1458. /* Reuse ptrace permission checks for now. */
  1459. err = -EACCES;
  1460. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1461. goto errout;
  1462. retry:
  1463. ctx = perf_lock_task_context(task, &flags);
  1464. if (ctx) {
  1465. unclone_ctx(ctx);
  1466. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1467. }
  1468. if (!ctx) {
  1469. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  1470. err = -ENOMEM;
  1471. if (!ctx)
  1472. goto errout;
  1473. __perf_event_init_context(ctx, task);
  1474. get_ctx(ctx);
  1475. if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
  1476. /*
  1477. * We raced with some other task; use
  1478. * the context they set.
  1479. */
  1480. kfree(ctx);
  1481. goto retry;
  1482. }
  1483. get_task_struct(task);
  1484. }
  1485. put_task_struct(task);
  1486. return ctx;
  1487. errout:
  1488. put_task_struct(task);
  1489. return ERR_PTR(err);
  1490. }
  1491. static void perf_event_free_filter(struct perf_event *event);
  1492. static void free_event_rcu(struct rcu_head *head)
  1493. {
  1494. struct perf_event *event;
  1495. event = container_of(head, struct perf_event, rcu_head);
  1496. if (event->ns)
  1497. put_pid_ns(event->ns);
  1498. perf_event_free_filter(event);
  1499. kfree(event);
  1500. }
  1501. static void perf_pending_sync(struct perf_event *event);
  1502. static void free_event(struct perf_event *event)
  1503. {
  1504. perf_pending_sync(event);
  1505. if (!event->parent) {
  1506. atomic_dec(&nr_events);
  1507. if (event->attr.mmap)
  1508. atomic_dec(&nr_mmap_events);
  1509. if (event->attr.comm)
  1510. atomic_dec(&nr_comm_events);
  1511. if (event->attr.task)
  1512. atomic_dec(&nr_task_events);
  1513. }
  1514. if (event->output) {
  1515. fput(event->output->filp);
  1516. event->output = NULL;
  1517. }
  1518. if (event->destroy)
  1519. event->destroy(event);
  1520. put_ctx(event->ctx);
  1521. call_rcu(&event->rcu_head, free_event_rcu);
  1522. }
  1523. int perf_event_release_kernel(struct perf_event *event)
  1524. {
  1525. struct perf_event_context *ctx = event->ctx;
  1526. WARN_ON_ONCE(ctx->parent_ctx);
  1527. mutex_lock(&ctx->mutex);
  1528. perf_event_remove_from_context(event);
  1529. mutex_unlock(&ctx->mutex);
  1530. mutex_lock(&event->owner->perf_event_mutex);
  1531. list_del_init(&event->owner_entry);
  1532. mutex_unlock(&event->owner->perf_event_mutex);
  1533. put_task_struct(event->owner);
  1534. free_event(event);
  1535. return 0;
  1536. }
  1537. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  1538. /*
  1539. * Called when the last reference to the file is gone.
  1540. */
  1541. static int perf_release(struct inode *inode, struct file *file)
  1542. {
  1543. struct perf_event *event = file->private_data;
  1544. file->private_data = NULL;
  1545. return perf_event_release_kernel(event);
  1546. }
  1547. static int perf_event_read_size(struct perf_event *event)
  1548. {
  1549. int entry = sizeof(u64); /* value */
  1550. int size = 0;
  1551. int nr = 1;
  1552. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1553. size += sizeof(u64);
  1554. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1555. size += sizeof(u64);
  1556. if (event->attr.read_format & PERF_FORMAT_ID)
  1557. entry += sizeof(u64);
  1558. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1559. nr += event->group_leader->nr_siblings;
  1560. size += sizeof(u64);
  1561. }
  1562. size += entry * nr;
  1563. return size;
  1564. }
  1565. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  1566. {
  1567. struct perf_event *child;
  1568. u64 total = 0;
  1569. *enabled = 0;
  1570. *running = 0;
  1571. mutex_lock(&event->child_mutex);
  1572. total += perf_event_read(event);
  1573. *enabled += event->total_time_enabled +
  1574. atomic64_read(&event->child_total_time_enabled);
  1575. *running += event->total_time_running +
  1576. atomic64_read(&event->child_total_time_running);
  1577. list_for_each_entry(child, &event->child_list, child_list) {
  1578. total += perf_event_read(child);
  1579. *enabled += child->total_time_enabled;
  1580. *running += child->total_time_running;
  1581. }
  1582. mutex_unlock(&event->child_mutex);
  1583. return total;
  1584. }
  1585. EXPORT_SYMBOL_GPL(perf_event_read_value);
  1586. static int perf_event_read_group(struct perf_event *event,
  1587. u64 read_format, char __user *buf)
  1588. {
  1589. struct perf_event *leader = event->group_leader, *sub;
  1590. int n = 0, size = 0, ret = -EFAULT;
  1591. struct perf_event_context *ctx = leader->ctx;
  1592. u64 values[5];
  1593. u64 count, enabled, running;
  1594. mutex_lock(&ctx->mutex);
  1595. count = perf_event_read_value(leader, &enabled, &running);
  1596. values[n++] = 1 + leader->nr_siblings;
  1597. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1598. values[n++] = enabled;
  1599. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1600. values[n++] = running;
  1601. values[n++] = count;
  1602. if (read_format & PERF_FORMAT_ID)
  1603. values[n++] = primary_event_id(leader);
  1604. size = n * sizeof(u64);
  1605. if (copy_to_user(buf, values, size))
  1606. goto unlock;
  1607. ret = size;
  1608. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1609. n = 0;
  1610. values[n++] = perf_event_read_value(sub, &enabled, &running);
  1611. if (read_format & PERF_FORMAT_ID)
  1612. values[n++] = primary_event_id(sub);
  1613. size = n * sizeof(u64);
  1614. if (copy_to_user(buf + ret, values, size)) {
  1615. ret = -EFAULT;
  1616. goto unlock;
  1617. }
  1618. ret += size;
  1619. }
  1620. unlock:
  1621. mutex_unlock(&ctx->mutex);
  1622. return ret;
  1623. }
  1624. static int perf_event_read_one(struct perf_event *event,
  1625. u64 read_format, char __user *buf)
  1626. {
  1627. u64 enabled, running;
  1628. u64 values[4];
  1629. int n = 0;
  1630. values[n++] = perf_event_read_value(event, &enabled, &running);
  1631. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1632. values[n++] = enabled;
  1633. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1634. values[n++] = running;
  1635. if (read_format & PERF_FORMAT_ID)
  1636. values[n++] = primary_event_id(event);
  1637. if (copy_to_user(buf, values, n * sizeof(u64)))
  1638. return -EFAULT;
  1639. return n * sizeof(u64);
  1640. }
  1641. /*
  1642. * Read the performance event - simple non blocking version for now
  1643. */
  1644. static ssize_t
  1645. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  1646. {
  1647. u64 read_format = event->attr.read_format;
  1648. int ret;
  1649. /*
  1650. * Return end-of-file for a read on a event that is in
  1651. * error state (i.e. because it was pinned but it couldn't be
  1652. * scheduled on to the CPU at some point).
  1653. */
  1654. if (event->state == PERF_EVENT_STATE_ERROR)
  1655. return 0;
  1656. if (count < perf_event_read_size(event))
  1657. return -ENOSPC;
  1658. WARN_ON_ONCE(event->ctx->parent_ctx);
  1659. if (read_format & PERF_FORMAT_GROUP)
  1660. ret = perf_event_read_group(event, read_format, buf);
  1661. else
  1662. ret = perf_event_read_one(event, read_format, buf);
  1663. return ret;
  1664. }
  1665. static ssize_t
  1666. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1667. {
  1668. struct perf_event *event = file->private_data;
  1669. return perf_read_hw(event, buf, count);
  1670. }
  1671. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1672. {
  1673. struct perf_event *event = file->private_data;
  1674. struct perf_mmap_data *data;
  1675. unsigned int events = POLL_HUP;
  1676. rcu_read_lock();
  1677. data = rcu_dereference(event->data);
  1678. if (data)
  1679. events = atomic_xchg(&data->poll, 0);
  1680. rcu_read_unlock();
  1681. poll_wait(file, &event->waitq, wait);
  1682. return events;
  1683. }
  1684. static void perf_event_reset(struct perf_event *event)
  1685. {
  1686. (void)perf_event_read(event);
  1687. atomic64_set(&event->count, 0);
  1688. perf_event_update_userpage(event);
  1689. }
  1690. /*
  1691. * Holding the top-level event's child_mutex means that any
  1692. * descendant process that has inherited this event will block
  1693. * in sync_child_event if it goes to exit, thus satisfying the
  1694. * task existence requirements of perf_event_enable/disable.
  1695. */
  1696. static void perf_event_for_each_child(struct perf_event *event,
  1697. void (*func)(struct perf_event *))
  1698. {
  1699. struct perf_event *child;
  1700. WARN_ON_ONCE(event->ctx->parent_ctx);
  1701. mutex_lock(&event->child_mutex);
  1702. func(event);
  1703. list_for_each_entry(child, &event->child_list, child_list)
  1704. func(child);
  1705. mutex_unlock(&event->child_mutex);
  1706. }
  1707. static void perf_event_for_each(struct perf_event *event,
  1708. void (*func)(struct perf_event *))
  1709. {
  1710. struct perf_event_context *ctx = event->ctx;
  1711. struct perf_event *sibling;
  1712. WARN_ON_ONCE(ctx->parent_ctx);
  1713. mutex_lock(&ctx->mutex);
  1714. event = event->group_leader;
  1715. perf_event_for_each_child(event, func);
  1716. func(event);
  1717. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  1718. perf_event_for_each_child(event, func);
  1719. mutex_unlock(&ctx->mutex);
  1720. }
  1721. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  1722. {
  1723. struct perf_event_context *ctx = event->ctx;
  1724. unsigned long size;
  1725. int ret = 0;
  1726. u64 value;
  1727. if (!event->attr.sample_period)
  1728. return -EINVAL;
  1729. size = copy_from_user(&value, arg, sizeof(value));
  1730. if (size != sizeof(value))
  1731. return -EFAULT;
  1732. if (!value)
  1733. return -EINVAL;
  1734. raw_spin_lock_irq(&ctx->lock);
  1735. if (event->attr.freq) {
  1736. if (value > sysctl_perf_event_sample_rate) {
  1737. ret = -EINVAL;
  1738. goto unlock;
  1739. }
  1740. event->attr.sample_freq = value;
  1741. } else {
  1742. event->attr.sample_period = value;
  1743. event->hw.sample_period = value;
  1744. }
  1745. unlock:
  1746. raw_spin_unlock_irq(&ctx->lock);
  1747. return ret;
  1748. }
  1749. static int perf_event_set_output(struct perf_event *event, int output_fd);
  1750. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  1751. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1752. {
  1753. struct perf_event *event = file->private_data;
  1754. void (*func)(struct perf_event *);
  1755. u32 flags = arg;
  1756. switch (cmd) {
  1757. case PERF_EVENT_IOC_ENABLE:
  1758. func = perf_event_enable;
  1759. break;
  1760. case PERF_EVENT_IOC_DISABLE:
  1761. func = perf_event_disable;
  1762. break;
  1763. case PERF_EVENT_IOC_RESET:
  1764. func = perf_event_reset;
  1765. break;
  1766. case PERF_EVENT_IOC_REFRESH:
  1767. return perf_event_refresh(event, arg);
  1768. case PERF_EVENT_IOC_PERIOD:
  1769. return perf_event_period(event, (u64 __user *)arg);
  1770. case PERF_EVENT_IOC_SET_OUTPUT:
  1771. return perf_event_set_output(event, arg);
  1772. case PERF_EVENT_IOC_SET_FILTER:
  1773. return perf_event_set_filter(event, (void __user *)arg);
  1774. default:
  1775. return -ENOTTY;
  1776. }
  1777. if (flags & PERF_IOC_FLAG_GROUP)
  1778. perf_event_for_each(event, func);
  1779. else
  1780. perf_event_for_each_child(event, func);
  1781. return 0;
  1782. }
  1783. int perf_event_task_enable(void)
  1784. {
  1785. struct perf_event *event;
  1786. mutex_lock(&current->perf_event_mutex);
  1787. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  1788. perf_event_for_each_child(event, perf_event_enable);
  1789. mutex_unlock(&current->perf_event_mutex);
  1790. return 0;
  1791. }
  1792. int perf_event_task_disable(void)
  1793. {
  1794. struct perf_event *event;
  1795. mutex_lock(&current->perf_event_mutex);
  1796. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  1797. perf_event_for_each_child(event, perf_event_disable);
  1798. mutex_unlock(&current->perf_event_mutex);
  1799. return 0;
  1800. }
  1801. #ifndef PERF_EVENT_INDEX_OFFSET
  1802. # define PERF_EVENT_INDEX_OFFSET 0
  1803. #endif
  1804. static int perf_event_index(struct perf_event *event)
  1805. {
  1806. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1807. return 0;
  1808. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  1809. }
  1810. /*
  1811. * Callers need to ensure there can be no nesting of this function, otherwise
  1812. * the seqlock logic goes bad. We can not serialize this because the arch
  1813. * code calls this from NMI context.
  1814. */
  1815. void perf_event_update_userpage(struct perf_event *event)
  1816. {
  1817. struct perf_event_mmap_page *userpg;
  1818. struct perf_mmap_data *data;
  1819. rcu_read_lock();
  1820. data = rcu_dereference(event->data);
  1821. if (!data)
  1822. goto unlock;
  1823. userpg = data->user_page;
  1824. /*
  1825. * Disable preemption so as to not let the corresponding user-space
  1826. * spin too long if we get preempted.
  1827. */
  1828. preempt_disable();
  1829. ++userpg->lock;
  1830. barrier();
  1831. userpg->index = perf_event_index(event);
  1832. userpg->offset = atomic64_read(&event->count);
  1833. if (event->state == PERF_EVENT_STATE_ACTIVE)
  1834. userpg->offset -= atomic64_read(&event->hw.prev_count);
  1835. userpg->time_enabled = event->total_time_enabled +
  1836. atomic64_read(&event->child_total_time_enabled);
  1837. userpg->time_running = event->total_time_running +
  1838. atomic64_read(&event->child_total_time_running);
  1839. barrier();
  1840. ++userpg->lock;
  1841. preempt_enable();
  1842. unlock:
  1843. rcu_read_unlock();
  1844. }
  1845. static unsigned long perf_data_size(struct perf_mmap_data *data)
  1846. {
  1847. return data->nr_pages << (PAGE_SHIFT + data->data_order);
  1848. }
  1849. #ifndef CONFIG_PERF_USE_VMALLOC
  1850. /*
  1851. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  1852. */
  1853. static struct page *
  1854. perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
  1855. {
  1856. if (pgoff > data->nr_pages)
  1857. return NULL;
  1858. if (pgoff == 0)
  1859. return virt_to_page(data->user_page);
  1860. return virt_to_page(data->data_pages[pgoff - 1]);
  1861. }
  1862. static struct perf_mmap_data *
  1863. perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
  1864. {
  1865. struct perf_mmap_data *data;
  1866. unsigned long size;
  1867. int i;
  1868. WARN_ON(atomic_read(&event->mmap_count));
  1869. size = sizeof(struct perf_mmap_data);
  1870. size += nr_pages * sizeof(void *);
  1871. data = kzalloc(size, GFP_KERNEL);
  1872. if (!data)
  1873. goto fail;
  1874. data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
  1875. if (!data->user_page)
  1876. goto fail_user_page;
  1877. for (i = 0; i < nr_pages; i++) {
  1878. data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
  1879. if (!data->data_pages[i])
  1880. goto fail_data_pages;
  1881. }
  1882. data->data_order = 0;
  1883. data->nr_pages = nr_pages;
  1884. return data;
  1885. fail_data_pages:
  1886. for (i--; i >= 0; i--)
  1887. free_page((unsigned long)data->data_pages[i]);
  1888. free_page((unsigned long)data->user_page);
  1889. fail_user_page:
  1890. kfree(data);
  1891. fail:
  1892. return NULL;
  1893. }
  1894. static void perf_mmap_free_page(unsigned long addr)
  1895. {
  1896. struct page *page = virt_to_page((void *)addr);
  1897. page->mapping = NULL;
  1898. __free_page(page);
  1899. }
  1900. static void perf_mmap_data_free(struct perf_mmap_data *data)
  1901. {
  1902. int i;
  1903. perf_mmap_free_page((unsigned long)data->user_page);
  1904. for (i = 0; i < data->nr_pages; i++)
  1905. perf_mmap_free_page((unsigned long)data->data_pages[i]);
  1906. kfree(data);
  1907. }
  1908. #else
  1909. /*
  1910. * Back perf_mmap() with vmalloc memory.
  1911. *
  1912. * Required for architectures that have d-cache aliasing issues.
  1913. */
  1914. static struct page *
  1915. perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
  1916. {
  1917. if (pgoff > (1UL << data->data_order))
  1918. return NULL;
  1919. return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
  1920. }
  1921. static void perf_mmap_unmark_page(void *addr)
  1922. {
  1923. struct page *page = vmalloc_to_page(addr);
  1924. page->mapping = NULL;
  1925. }
  1926. static void perf_mmap_data_free_work(struct work_struct *work)
  1927. {
  1928. struct perf_mmap_data *data;
  1929. void *base;
  1930. int i, nr;
  1931. data = container_of(work, struct perf_mmap_data, work);
  1932. nr = 1 << data->data_order;
  1933. base = data->user_page;
  1934. for (i = 0; i < nr + 1; i++)
  1935. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  1936. vfree(base);
  1937. kfree(data);
  1938. }
  1939. static void perf_mmap_data_free(struct perf_mmap_data *data)
  1940. {
  1941. schedule_work(&data->work);
  1942. }
  1943. static struct perf_mmap_data *
  1944. perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
  1945. {
  1946. struct perf_mmap_data *data;
  1947. unsigned long size;
  1948. void *all_buf;
  1949. WARN_ON(atomic_read(&event->mmap_count));
  1950. size = sizeof(struct perf_mmap_data);
  1951. size += sizeof(void *);
  1952. data = kzalloc(size, GFP_KERNEL);
  1953. if (!data)
  1954. goto fail;
  1955. INIT_WORK(&data->work, perf_mmap_data_free_work);
  1956. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  1957. if (!all_buf)
  1958. goto fail_all_buf;
  1959. data->user_page = all_buf;
  1960. data->data_pages[0] = all_buf + PAGE_SIZE;
  1961. data->data_order = ilog2(nr_pages);
  1962. data->nr_pages = 1;
  1963. return data;
  1964. fail_all_buf:
  1965. kfree(data);
  1966. fail:
  1967. return NULL;
  1968. }
  1969. #endif
  1970. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1971. {
  1972. struct perf_event *event = vma->vm_file->private_data;
  1973. struct perf_mmap_data *data;
  1974. int ret = VM_FAULT_SIGBUS;
  1975. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  1976. if (vmf->pgoff == 0)
  1977. ret = 0;
  1978. return ret;
  1979. }
  1980. rcu_read_lock();
  1981. data = rcu_dereference(event->data);
  1982. if (!data)
  1983. goto unlock;
  1984. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  1985. goto unlock;
  1986. vmf->page = perf_mmap_to_page(data, vmf->pgoff);
  1987. if (!vmf->page)
  1988. goto unlock;
  1989. get_page(vmf->page);
  1990. vmf->page->mapping = vma->vm_file->f_mapping;
  1991. vmf->page->index = vmf->pgoff;
  1992. ret = 0;
  1993. unlock:
  1994. rcu_read_unlock();
  1995. return ret;
  1996. }
  1997. static void
  1998. perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
  1999. {
  2000. long max_size = perf_data_size(data);
  2001. atomic_set(&data->lock, -1);
  2002. if (event->attr.watermark) {
  2003. data->watermark = min_t(long, max_size,
  2004. event->attr.wakeup_watermark);
  2005. }
  2006. if (!data->watermark)
  2007. data->watermark = max_size / 2;
  2008. rcu_assign_pointer(event->data, data);
  2009. }
  2010. static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
  2011. {
  2012. struct perf_mmap_data *data;
  2013. data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
  2014. perf_mmap_data_free(data);
  2015. }
  2016. static void perf_mmap_data_release(struct perf_event *event)
  2017. {
  2018. struct perf_mmap_data *data = event->data;
  2019. WARN_ON(atomic_read(&event->mmap_count));
  2020. rcu_assign_pointer(event->data, NULL);
  2021. call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
  2022. }
  2023. static void perf_mmap_open(struct vm_area_struct *vma)
  2024. {
  2025. struct perf_event *event = vma->vm_file->private_data;
  2026. atomic_inc(&event->mmap_count);
  2027. }
  2028. static void perf_mmap_close(struct vm_area_struct *vma)
  2029. {
  2030. struct perf_event *event = vma->vm_file->private_data;
  2031. WARN_ON_ONCE(event->ctx->parent_ctx);
  2032. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2033. unsigned long size = perf_data_size(event->data);
  2034. struct user_struct *user = current_user();
  2035. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2036. vma->vm_mm->locked_vm -= event->data->nr_locked;
  2037. perf_mmap_data_release(event);
  2038. mutex_unlock(&event->mmap_mutex);
  2039. }
  2040. }
  2041. static const struct vm_operations_struct perf_mmap_vmops = {
  2042. .open = perf_mmap_open,
  2043. .close = perf_mmap_close,
  2044. .fault = perf_mmap_fault,
  2045. .page_mkwrite = perf_mmap_fault,
  2046. };
  2047. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2048. {
  2049. struct perf_event *event = file->private_data;
  2050. unsigned long user_locked, user_lock_limit;
  2051. struct user_struct *user = current_user();
  2052. unsigned long locked, lock_limit;
  2053. struct perf_mmap_data *data;
  2054. unsigned long vma_size;
  2055. unsigned long nr_pages;
  2056. long user_extra, extra;
  2057. int ret = 0;
  2058. if (!(vma->vm_flags & VM_SHARED))
  2059. return -EINVAL;
  2060. vma_size = vma->vm_end - vma->vm_start;
  2061. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2062. /*
  2063. * If we have data pages ensure they're a power-of-two number, so we
  2064. * can do bitmasks instead of modulo.
  2065. */
  2066. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2067. return -EINVAL;
  2068. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2069. return -EINVAL;
  2070. if (vma->vm_pgoff != 0)
  2071. return -EINVAL;
  2072. WARN_ON_ONCE(event->ctx->parent_ctx);
  2073. mutex_lock(&event->mmap_mutex);
  2074. if (event->output) {
  2075. ret = -EINVAL;
  2076. goto unlock;
  2077. }
  2078. if (atomic_inc_not_zero(&event->mmap_count)) {
  2079. if (nr_pages != event->data->nr_pages)
  2080. ret = -EINVAL;
  2081. goto unlock;
  2082. }
  2083. user_extra = nr_pages + 1;
  2084. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2085. /*
  2086. * Increase the limit linearly with more CPUs:
  2087. */
  2088. user_lock_limit *= num_online_cpus();
  2089. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2090. extra = 0;
  2091. if (user_locked > user_lock_limit)
  2092. extra = user_locked - user_lock_limit;
  2093. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  2094. lock_limit >>= PAGE_SHIFT;
  2095. locked = vma->vm_mm->locked_vm + extra;
  2096. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2097. !capable(CAP_IPC_LOCK)) {
  2098. ret = -EPERM;
  2099. goto unlock;
  2100. }
  2101. WARN_ON(event->data);
  2102. data = perf_mmap_data_alloc(event, nr_pages);
  2103. ret = -ENOMEM;
  2104. if (!data)
  2105. goto unlock;
  2106. ret = 0;
  2107. perf_mmap_data_init(event, data);
  2108. atomic_set(&event->mmap_count, 1);
  2109. atomic_long_add(user_extra, &user->locked_vm);
  2110. vma->vm_mm->locked_vm += extra;
  2111. event->data->nr_locked = extra;
  2112. if (vma->vm_flags & VM_WRITE)
  2113. event->data->writable = 1;
  2114. unlock:
  2115. mutex_unlock(&event->mmap_mutex);
  2116. vma->vm_flags |= VM_RESERVED;
  2117. vma->vm_ops = &perf_mmap_vmops;
  2118. return ret;
  2119. }
  2120. static int perf_fasync(int fd, struct file *filp, int on)
  2121. {
  2122. struct inode *inode = filp->f_path.dentry->d_inode;
  2123. struct perf_event *event = filp->private_data;
  2124. int retval;
  2125. mutex_lock(&inode->i_mutex);
  2126. retval = fasync_helper(fd, filp, on, &event->fasync);
  2127. mutex_unlock(&inode->i_mutex);
  2128. if (retval < 0)
  2129. return retval;
  2130. return 0;
  2131. }
  2132. static const struct file_operations perf_fops = {
  2133. .release = perf_release,
  2134. .read = perf_read,
  2135. .poll = perf_poll,
  2136. .unlocked_ioctl = perf_ioctl,
  2137. .compat_ioctl = perf_ioctl,
  2138. .mmap = perf_mmap,
  2139. .fasync = perf_fasync,
  2140. };
  2141. /*
  2142. * Perf event wakeup
  2143. *
  2144. * If there's data, ensure we set the poll() state and publish everything
  2145. * to user-space before waking everybody up.
  2146. */
  2147. void perf_event_wakeup(struct perf_event *event)
  2148. {
  2149. wake_up_all(&event->waitq);
  2150. if (event->pending_kill) {
  2151. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2152. event->pending_kill = 0;
  2153. }
  2154. }
  2155. /*
  2156. * Pending wakeups
  2157. *
  2158. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  2159. *
  2160. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  2161. * single linked list and use cmpxchg() to add entries lockless.
  2162. */
  2163. static void perf_pending_event(struct perf_pending_entry *entry)
  2164. {
  2165. struct perf_event *event = container_of(entry,
  2166. struct perf_event, pending);
  2167. if (event->pending_disable) {
  2168. event->pending_disable = 0;
  2169. __perf_event_disable(event);
  2170. }
  2171. if (event->pending_wakeup) {
  2172. event->pending_wakeup = 0;
  2173. perf_event_wakeup(event);
  2174. }
  2175. }
  2176. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  2177. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  2178. PENDING_TAIL,
  2179. };
  2180. static void perf_pending_queue(struct perf_pending_entry *entry,
  2181. void (*func)(struct perf_pending_entry *))
  2182. {
  2183. struct perf_pending_entry **head;
  2184. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  2185. return;
  2186. entry->func = func;
  2187. head = &get_cpu_var(perf_pending_head);
  2188. do {
  2189. entry->next = *head;
  2190. } while (cmpxchg(head, entry->next, entry) != entry->next);
  2191. set_perf_event_pending();
  2192. put_cpu_var(perf_pending_head);
  2193. }
  2194. static int __perf_pending_run(void)
  2195. {
  2196. struct perf_pending_entry *list;
  2197. int nr = 0;
  2198. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  2199. while (list != PENDING_TAIL) {
  2200. void (*func)(struct perf_pending_entry *);
  2201. struct perf_pending_entry *entry = list;
  2202. list = list->next;
  2203. func = entry->func;
  2204. entry->next = NULL;
  2205. /*
  2206. * Ensure we observe the unqueue before we issue the wakeup,
  2207. * so that we won't be waiting forever.
  2208. * -- see perf_not_pending().
  2209. */
  2210. smp_wmb();
  2211. func(entry);
  2212. nr++;
  2213. }
  2214. return nr;
  2215. }
  2216. static inline int perf_not_pending(struct perf_event *event)
  2217. {
  2218. /*
  2219. * If we flush on whatever cpu we run, there is a chance we don't
  2220. * need to wait.
  2221. */
  2222. get_cpu();
  2223. __perf_pending_run();
  2224. put_cpu();
  2225. /*
  2226. * Ensure we see the proper queue state before going to sleep
  2227. * so that we do not miss the wakeup. -- see perf_pending_handle()
  2228. */
  2229. smp_rmb();
  2230. return event->pending.next == NULL;
  2231. }
  2232. static void perf_pending_sync(struct perf_event *event)
  2233. {
  2234. wait_event(event->waitq, perf_not_pending(event));
  2235. }
  2236. void perf_event_do_pending(void)
  2237. {
  2238. __perf_pending_run();
  2239. }
  2240. /*
  2241. * Callchain support -- arch specific
  2242. */
  2243. __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  2244. {
  2245. return NULL;
  2246. }
  2247. /*
  2248. * Output
  2249. */
  2250. static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
  2251. unsigned long offset, unsigned long head)
  2252. {
  2253. unsigned long mask;
  2254. if (!data->writable)
  2255. return true;
  2256. mask = perf_data_size(data) - 1;
  2257. offset = (offset - tail) & mask;
  2258. head = (head - tail) & mask;
  2259. if ((int)(head - offset) < 0)
  2260. return false;
  2261. return true;
  2262. }
  2263. static void perf_output_wakeup(struct perf_output_handle *handle)
  2264. {
  2265. atomic_set(&handle->data->poll, POLL_IN);
  2266. if (handle->nmi) {
  2267. handle->event->pending_wakeup = 1;
  2268. perf_pending_queue(&handle->event->pending,
  2269. perf_pending_event);
  2270. } else
  2271. perf_event_wakeup(handle->event);
  2272. }
  2273. /*
  2274. * Curious locking construct.
  2275. *
  2276. * We need to ensure a later event_id doesn't publish a head when a former
  2277. * event_id isn't done writing. However since we need to deal with NMIs we
  2278. * cannot fully serialize things.
  2279. *
  2280. * What we do is serialize between CPUs so we only have to deal with NMI
  2281. * nesting on a single CPU.
  2282. *
  2283. * We only publish the head (and generate a wakeup) when the outer-most
  2284. * event_id completes.
  2285. */
  2286. static void perf_output_lock(struct perf_output_handle *handle)
  2287. {
  2288. struct perf_mmap_data *data = handle->data;
  2289. int cur, cpu = get_cpu();
  2290. handle->locked = 0;
  2291. for (;;) {
  2292. cur = atomic_cmpxchg(&data->lock, -1, cpu);
  2293. if (cur == -1) {
  2294. handle->locked = 1;
  2295. break;
  2296. }
  2297. if (cur == cpu)
  2298. break;
  2299. cpu_relax();
  2300. }
  2301. }
  2302. static void perf_output_unlock(struct perf_output_handle *handle)
  2303. {
  2304. struct perf_mmap_data *data = handle->data;
  2305. unsigned long head;
  2306. int cpu;
  2307. data->done_head = data->head;
  2308. if (!handle->locked)
  2309. goto out;
  2310. again:
  2311. /*
  2312. * The xchg implies a full barrier that ensures all writes are done
  2313. * before we publish the new head, matched by a rmb() in userspace when
  2314. * reading this position.
  2315. */
  2316. while ((head = atomic_long_xchg(&data->done_head, 0)))
  2317. data->user_page->data_head = head;
  2318. /*
  2319. * NMI can happen here, which means we can miss a done_head update.
  2320. */
  2321. cpu = atomic_xchg(&data->lock, -1);
  2322. WARN_ON_ONCE(cpu != smp_processor_id());
  2323. /*
  2324. * Therefore we have to validate we did not indeed do so.
  2325. */
  2326. if (unlikely(atomic_long_read(&data->done_head))) {
  2327. /*
  2328. * Since we had it locked, we can lock it again.
  2329. */
  2330. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  2331. cpu_relax();
  2332. goto again;
  2333. }
  2334. if (atomic_xchg(&data->wakeup, 0))
  2335. perf_output_wakeup(handle);
  2336. out:
  2337. put_cpu();
  2338. }
  2339. void perf_output_copy(struct perf_output_handle *handle,
  2340. const void *buf, unsigned int len)
  2341. {
  2342. unsigned int pages_mask;
  2343. unsigned long offset;
  2344. unsigned int size;
  2345. void **pages;
  2346. offset = handle->offset;
  2347. pages_mask = handle->data->nr_pages - 1;
  2348. pages = handle->data->data_pages;
  2349. do {
  2350. unsigned long page_offset;
  2351. unsigned long page_size;
  2352. int nr;
  2353. nr = (offset >> PAGE_SHIFT) & pages_mask;
  2354. page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
  2355. page_offset = offset & (page_size - 1);
  2356. size = min_t(unsigned int, page_size - page_offset, len);
  2357. memcpy(pages[nr] + page_offset, buf, size);
  2358. len -= size;
  2359. buf += size;
  2360. offset += size;
  2361. } while (len);
  2362. handle->offset = offset;
  2363. /*
  2364. * Check we didn't copy past our reservation window, taking the
  2365. * possible unsigned int wrap into account.
  2366. */
  2367. WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
  2368. }
  2369. int perf_output_begin(struct perf_output_handle *handle,
  2370. struct perf_event *event, unsigned int size,
  2371. int nmi, int sample)
  2372. {
  2373. struct perf_event *output_event;
  2374. struct perf_mmap_data *data;
  2375. unsigned long tail, offset, head;
  2376. int have_lost;
  2377. struct {
  2378. struct perf_event_header header;
  2379. u64 id;
  2380. u64 lost;
  2381. } lost_event;
  2382. rcu_read_lock();
  2383. /*
  2384. * For inherited events we send all the output towards the parent.
  2385. */
  2386. if (event->parent)
  2387. event = event->parent;
  2388. output_event = rcu_dereference(event->output);
  2389. if (output_event)
  2390. event = output_event;
  2391. data = rcu_dereference(event->data);
  2392. if (!data)
  2393. goto out;
  2394. handle->data = data;
  2395. handle->event = event;
  2396. handle->nmi = nmi;
  2397. handle->sample = sample;
  2398. if (!data->nr_pages)
  2399. goto fail;
  2400. have_lost = atomic_read(&data->lost);
  2401. if (have_lost)
  2402. size += sizeof(lost_event);
  2403. perf_output_lock(handle);
  2404. do {
  2405. /*
  2406. * Userspace could choose to issue a mb() before updating the
  2407. * tail pointer. So that all reads will be completed before the
  2408. * write is issued.
  2409. */
  2410. tail = ACCESS_ONCE(data->user_page->data_tail);
  2411. smp_rmb();
  2412. offset = head = atomic_long_read(&data->head);
  2413. head += size;
  2414. if (unlikely(!perf_output_space(data, tail, offset, head)))
  2415. goto fail;
  2416. } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
  2417. handle->offset = offset;
  2418. handle->head = head;
  2419. if (head - tail > data->watermark)
  2420. atomic_set(&data->wakeup, 1);
  2421. if (have_lost) {
  2422. lost_event.header.type = PERF_RECORD_LOST;
  2423. lost_event.header.misc = 0;
  2424. lost_event.header.size = sizeof(lost_event);
  2425. lost_event.id = event->id;
  2426. lost_event.lost = atomic_xchg(&data->lost, 0);
  2427. perf_output_put(handle, lost_event);
  2428. }
  2429. return 0;
  2430. fail:
  2431. atomic_inc(&data->lost);
  2432. perf_output_unlock(handle);
  2433. out:
  2434. rcu_read_unlock();
  2435. return -ENOSPC;
  2436. }
  2437. void perf_output_end(struct perf_output_handle *handle)
  2438. {
  2439. struct perf_event *event = handle->event;
  2440. struct perf_mmap_data *data = handle->data;
  2441. int wakeup_events = event->attr.wakeup_events;
  2442. if (handle->sample && wakeup_events) {
  2443. int events = atomic_inc_return(&data->events);
  2444. if (events >= wakeup_events) {
  2445. atomic_sub(wakeup_events, &data->events);
  2446. atomic_set(&data->wakeup, 1);
  2447. }
  2448. }
  2449. perf_output_unlock(handle);
  2450. rcu_read_unlock();
  2451. }
  2452. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  2453. {
  2454. /*
  2455. * only top level events have the pid namespace they were created in
  2456. */
  2457. if (event->parent)
  2458. event = event->parent;
  2459. return task_tgid_nr_ns(p, event->ns);
  2460. }
  2461. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  2462. {
  2463. /*
  2464. * only top level events have the pid namespace they were created in
  2465. */
  2466. if (event->parent)
  2467. event = event->parent;
  2468. return task_pid_nr_ns(p, event->ns);
  2469. }
  2470. static void perf_output_read_one(struct perf_output_handle *handle,
  2471. struct perf_event *event)
  2472. {
  2473. u64 read_format = event->attr.read_format;
  2474. u64 values[4];
  2475. int n = 0;
  2476. values[n++] = atomic64_read(&event->count);
  2477. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2478. values[n++] = event->total_time_enabled +
  2479. atomic64_read(&event->child_total_time_enabled);
  2480. }
  2481. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2482. values[n++] = event->total_time_running +
  2483. atomic64_read(&event->child_total_time_running);
  2484. }
  2485. if (read_format & PERF_FORMAT_ID)
  2486. values[n++] = primary_event_id(event);
  2487. perf_output_copy(handle, values, n * sizeof(u64));
  2488. }
  2489. /*
  2490. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2491. */
  2492. static void perf_output_read_group(struct perf_output_handle *handle,
  2493. struct perf_event *event)
  2494. {
  2495. struct perf_event *leader = event->group_leader, *sub;
  2496. u64 read_format = event->attr.read_format;
  2497. u64 values[5];
  2498. int n = 0;
  2499. values[n++] = 1 + leader->nr_siblings;
  2500. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2501. values[n++] = leader->total_time_enabled;
  2502. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2503. values[n++] = leader->total_time_running;
  2504. if (leader != event)
  2505. leader->pmu->read(leader);
  2506. values[n++] = atomic64_read(&leader->count);
  2507. if (read_format & PERF_FORMAT_ID)
  2508. values[n++] = primary_event_id(leader);
  2509. perf_output_copy(handle, values, n * sizeof(u64));
  2510. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2511. n = 0;
  2512. if (sub != event)
  2513. sub->pmu->read(sub);
  2514. values[n++] = atomic64_read(&sub->count);
  2515. if (read_format & PERF_FORMAT_ID)
  2516. values[n++] = primary_event_id(sub);
  2517. perf_output_copy(handle, values, n * sizeof(u64));
  2518. }
  2519. }
  2520. static void perf_output_read(struct perf_output_handle *handle,
  2521. struct perf_event *event)
  2522. {
  2523. if (event->attr.read_format & PERF_FORMAT_GROUP)
  2524. perf_output_read_group(handle, event);
  2525. else
  2526. perf_output_read_one(handle, event);
  2527. }
  2528. void perf_output_sample(struct perf_output_handle *handle,
  2529. struct perf_event_header *header,
  2530. struct perf_sample_data *data,
  2531. struct perf_event *event)
  2532. {
  2533. u64 sample_type = data->type;
  2534. perf_output_put(handle, *header);
  2535. if (sample_type & PERF_SAMPLE_IP)
  2536. perf_output_put(handle, data->ip);
  2537. if (sample_type & PERF_SAMPLE_TID)
  2538. perf_output_put(handle, data->tid_entry);
  2539. if (sample_type & PERF_SAMPLE_TIME)
  2540. perf_output_put(handle, data->time);
  2541. if (sample_type & PERF_SAMPLE_ADDR)
  2542. perf_output_put(handle, data->addr);
  2543. if (sample_type & PERF_SAMPLE_ID)
  2544. perf_output_put(handle, data->id);
  2545. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2546. perf_output_put(handle, data->stream_id);
  2547. if (sample_type & PERF_SAMPLE_CPU)
  2548. perf_output_put(handle, data->cpu_entry);
  2549. if (sample_type & PERF_SAMPLE_PERIOD)
  2550. perf_output_put(handle, data->period);
  2551. if (sample_type & PERF_SAMPLE_READ)
  2552. perf_output_read(handle, event);
  2553. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2554. if (data->callchain) {
  2555. int size = 1;
  2556. if (data->callchain)
  2557. size += data->callchain->nr;
  2558. size *= sizeof(u64);
  2559. perf_output_copy(handle, data->callchain, size);
  2560. } else {
  2561. u64 nr = 0;
  2562. perf_output_put(handle, nr);
  2563. }
  2564. }
  2565. if (sample_type & PERF_SAMPLE_RAW) {
  2566. if (data->raw) {
  2567. perf_output_put(handle, data->raw->size);
  2568. perf_output_copy(handle, data->raw->data,
  2569. data->raw->size);
  2570. } else {
  2571. struct {
  2572. u32 size;
  2573. u32 data;
  2574. } raw = {
  2575. .size = sizeof(u32),
  2576. .data = 0,
  2577. };
  2578. perf_output_put(handle, raw);
  2579. }
  2580. }
  2581. }
  2582. void perf_prepare_sample(struct perf_event_header *header,
  2583. struct perf_sample_data *data,
  2584. struct perf_event *event,
  2585. struct pt_regs *regs)
  2586. {
  2587. u64 sample_type = event->attr.sample_type;
  2588. data->type = sample_type;
  2589. header->type = PERF_RECORD_SAMPLE;
  2590. header->size = sizeof(*header);
  2591. header->misc = 0;
  2592. header->misc |= perf_misc_flags(regs);
  2593. if (sample_type & PERF_SAMPLE_IP) {
  2594. data->ip = perf_instruction_pointer(regs);
  2595. header->size += sizeof(data->ip);
  2596. }
  2597. if (sample_type & PERF_SAMPLE_TID) {
  2598. /* namespace issues */
  2599. data->tid_entry.pid = perf_event_pid(event, current);
  2600. data->tid_entry.tid = perf_event_tid(event, current);
  2601. header->size += sizeof(data->tid_entry);
  2602. }
  2603. if (sample_type & PERF_SAMPLE_TIME) {
  2604. data->time = perf_clock();
  2605. header->size += sizeof(data->time);
  2606. }
  2607. if (sample_type & PERF_SAMPLE_ADDR)
  2608. header->size += sizeof(data->addr);
  2609. if (sample_type & PERF_SAMPLE_ID) {
  2610. data->id = primary_event_id(event);
  2611. header->size += sizeof(data->id);
  2612. }
  2613. if (sample_type & PERF_SAMPLE_STREAM_ID) {
  2614. data->stream_id = event->id;
  2615. header->size += sizeof(data->stream_id);
  2616. }
  2617. if (sample_type & PERF_SAMPLE_CPU) {
  2618. data->cpu_entry.cpu = raw_smp_processor_id();
  2619. data->cpu_entry.reserved = 0;
  2620. header->size += sizeof(data->cpu_entry);
  2621. }
  2622. if (sample_type & PERF_SAMPLE_PERIOD)
  2623. header->size += sizeof(data->period);
  2624. if (sample_type & PERF_SAMPLE_READ)
  2625. header->size += perf_event_read_size(event);
  2626. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2627. int size = 1;
  2628. data->callchain = perf_callchain(regs);
  2629. if (data->callchain)
  2630. size += data->callchain->nr;
  2631. header->size += size * sizeof(u64);
  2632. }
  2633. if (sample_type & PERF_SAMPLE_RAW) {
  2634. int size = sizeof(u32);
  2635. if (data->raw)
  2636. size += data->raw->size;
  2637. else
  2638. size += sizeof(u32);
  2639. WARN_ON_ONCE(size & (sizeof(u64)-1));
  2640. header->size += size;
  2641. }
  2642. }
  2643. static void perf_event_output(struct perf_event *event, int nmi,
  2644. struct perf_sample_data *data,
  2645. struct pt_regs *regs)
  2646. {
  2647. struct perf_output_handle handle;
  2648. struct perf_event_header header;
  2649. perf_prepare_sample(&header, data, event, regs);
  2650. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  2651. return;
  2652. perf_output_sample(&handle, &header, data, event);
  2653. perf_output_end(&handle);
  2654. }
  2655. /*
  2656. * read event_id
  2657. */
  2658. struct perf_read_event {
  2659. struct perf_event_header header;
  2660. u32 pid;
  2661. u32 tid;
  2662. };
  2663. static void
  2664. perf_event_read_event(struct perf_event *event,
  2665. struct task_struct *task)
  2666. {
  2667. struct perf_output_handle handle;
  2668. struct perf_read_event read_event = {
  2669. .header = {
  2670. .type = PERF_RECORD_READ,
  2671. .misc = 0,
  2672. .size = sizeof(read_event) + perf_event_read_size(event),
  2673. },
  2674. .pid = perf_event_pid(event, task),
  2675. .tid = perf_event_tid(event, task),
  2676. };
  2677. int ret;
  2678. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  2679. if (ret)
  2680. return;
  2681. perf_output_put(&handle, read_event);
  2682. perf_output_read(&handle, event);
  2683. perf_output_end(&handle);
  2684. }
  2685. /*
  2686. * task tracking -- fork/exit
  2687. *
  2688. * enabled by: attr.comm | attr.mmap | attr.task
  2689. */
  2690. struct perf_task_event {
  2691. struct task_struct *task;
  2692. struct perf_event_context *task_ctx;
  2693. struct {
  2694. struct perf_event_header header;
  2695. u32 pid;
  2696. u32 ppid;
  2697. u32 tid;
  2698. u32 ptid;
  2699. u64 time;
  2700. } event_id;
  2701. };
  2702. static void perf_event_task_output(struct perf_event *event,
  2703. struct perf_task_event *task_event)
  2704. {
  2705. struct perf_output_handle handle;
  2706. int size;
  2707. struct task_struct *task = task_event->task;
  2708. int ret;
  2709. size = task_event->event_id.header.size;
  2710. ret = perf_output_begin(&handle, event, size, 0, 0);
  2711. if (ret)
  2712. return;
  2713. task_event->event_id.pid = perf_event_pid(event, task);
  2714. task_event->event_id.ppid = perf_event_pid(event, current);
  2715. task_event->event_id.tid = perf_event_tid(event, task);
  2716. task_event->event_id.ptid = perf_event_tid(event, current);
  2717. task_event->event_id.time = perf_clock();
  2718. perf_output_put(&handle, task_event->event_id);
  2719. perf_output_end(&handle);
  2720. }
  2721. static int perf_event_task_match(struct perf_event *event)
  2722. {
  2723. if (event->cpu != -1 && event->cpu != smp_processor_id())
  2724. return 0;
  2725. if (event->attr.comm || event->attr.mmap || event->attr.task)
  2726. return 1;
  2727. return 0;
  2728. }
  2729. static void perf_event_task_ctx(struct perf_event_context *ctx,
  2730. struct perf_task_event *task_event)
  2731. {
  2732. struct perf_event *event;
  2733. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2734. if (perf_event_task_match(event))
  2735. perf_event_task_output(event, task_event);
  2736. }
  2737. }
  2738. static void perf_event_task_event(struct perf_task_event *task_event)
  2739. {
  2740. struct perf_cpu_context *cpuctx;
  2741. struct perf_event_context *ctx = task_event->task_ctx;
  2742. rcu_read_lock();
  2743. cpuctx = &get_cpu_var(perf_cpu_context);
  2744. perf_event_task_ctx(&cpuctx->ctx, task_event);
  2745. if (!ctx)
  2746. ctx = rcu_dereference(task_event->task->perf_event_ctxp);
  2747. if (ctx)
  2748. perf_event_task_ctx(ctx, task_event);
  2749. put_cpu_var(perf_cpu_context);
  2750. rcu_read_unlock();
  2751. }
  2752. static void perf_event_task(struct task_struct *task,
  2753. struct perf_event_context *task_ctx,
  2754. int new)
  2755. {
  2756. struct perf_task_event task_event;
  2757. if (!atomic_read(&nr_comm_events) &&
  2758. !atomic_read(&nr_mmap_events) &&
  2759. !atomic_read(&nr_task_events))
  2760. return;
  2761. task_event = (struct perf_task_event){
  2762. .task = task,
  2763. .task_ctx = task_ctx,
  2764. .event_id = {
  2765. .header = {
  2766. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  2767. .misc = 0,
  2768. .size = sizeof(task_event.event_id),
  2769. },
  2770. /* .pid */
  2771. /* .ppid */
  2772. /* .tid */
  2773. /* .ptid */
  2774. },
  2775. };
  2776. perf_event_task_event(&task_event);
  2777. }
  2778. void perf_event_fork(struct task_struct *task)
  2779. {
  2780. perf_event_task(task, NULL, 1);
  2781. }
  2782. /*
  2783. * comm tracking
  2784. */
  2785. struct perf_comm_event {
  2786. struct task_struct *task;
  2787. char *comm;
  2788. int comm_size;
  2789. struct {
  2790. struct perf_event_header header;
  2791. u32 pid;
  2792. u32 tid;
  2793. } event_id;
  2794. };
  2795. static void perf_event_comm_output(struct perf_event *event,
  2796. struct perf_comm_event *comm_event)
  2797. {
  2798. struct perf_output_handle handle;
  2799. int size = comm_event->event_id.header.size;
  2800. int ret = perf_output_begin(&handle, event, size, 0, 0);
  2801. if (ret)
  2802. return;
  2803. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  2804. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  2805. perf_output_put(&handle, comm_event->event_id);
  2806. perf_output_copy(&handle, comm_event->comm,
  2807. comm_event->comm_size);
  2808. perf_output_end(&handle);
  2809. }
  2810. static int perf_event_comm_match(struct perf_event *event)
  2811. {
  2812. if (event->cpu != -1 && event->cpu != smp_processor_id())
  2813. return 0;
  2814. if (event->attr.comm)
  2815. return 1;
  2816. return 0;
  2817. }
  2818. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  2819. struct perf_comm_event *comm_event)
  2820. {
  2821. struct perf_event *event;
  2822. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2823. if (perf_event_comm_match(event))
  2824. perf_event_comm_output(event, comm_event);
  2825. }
  2826. }
  2827. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  2828. {
  2829. struct perf_cpu_context *cpuctx;
  2830. struct perf_event_context *ctx;
  2831. unsigned int size;
  2832. char comm[TASK_COMM_LEN];
  2833. memset(comm, 0, sizeof(comm));
  2834. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  2835. size = ALIGN(strlen(comm)+1, sizeof(u64));
  2836. comm_event->comm = comm;
  2837. comm_event->comm_size = size;
  2838. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  2839. rcu_read_lock();
  2840. cpuctx = &get_cpu_var(perf_cpu_context);
  2841. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  2842. ctx = rcu_dereference(current->perf_event_ctxp);
  2843. if (ctx)
  2844. perf_event_comm_ctx(ctx, comm_event);
  2845. put_cpu_var(perf_cpu_context);
  2846. rcu_read_unlock();
  2847. }
  2848. void perf_event_comm(struct task_struct *task)
  2849. {
  2850. struct perf_comm_event comm_event;
  2851. if (task->perf_event_ctxp)
  2852. perf_event_enable_on_exec(task);
  2853. if (!atomic_read(&nr_comm_events))
  2854. return;
  2855. comm_event = (struct perf_comm_event){
  2856. .task = task,
  2857. /* .comm */
  2858. /* .comm_size */
  2859. .event_id = {
  2860. .header = {
  2861. .type = PERF_RECORD_COMM,
  2862. .misc = 0,
  2863. /* .size */
  2864. },
  2865. /* .pid */
  2866. /* .tid */
  2867. },
  2868. };
  2869. perf_event_comm_event(&comm_event);
  2870. }
  2871. /*
  2872. * mmap tracking
  2873. */
  2874. struct perf_mmap_event {
  2875. struct vm_area_struct *vma;
  2876. const char *file_name;
  2877. int file_size;
  2878. struct {
  2879. struct perf_event_header header;
  2880. u32 pid;
  2881. u32 tid;
  2882. u64 start;
  2883. u64 len;
  2884. u64 pgoff;
  2885. } event_id;
  2886. };
  2887. static void perf_event_mmap_output(struct perf_event *event,
  2888. struct perf_mmap_event *mmap_event)
  2889. {
  2890. struct perf_output_handle handle;
  2891. int size = mmap_event->event_id.header.size;
  2892. int ret = perf_output_begin(&handle, event, size, 0, 0);
  2893. if (ret)
  2894. return;
  2895. mmap_event->event_id.pid = perf_event_pid(event, current);
  2896. mmap_event->event_id.tid = perf_event_tid(event, current);
  2897. perf_output_put(&handle, mmap_event->event_id);
  2898. perf_output_copy(&handle, mmap_event->file_name,
  2899. mmap_event->file_size);
  2900. perf_output_end(&handle);
  2901. }
  2902. static int perf_event_mmap_match(struct perf_event *event,
  2903. struct perf_mmap_event *mmap_event)
  2904. {
  2905. if (event->cpu != -1 && event->cpu != smp_processor_id())
  2906. return 0;
  2907. if (event->attr.mmap)
  2908. return 1;
  2909. return 0;
  2910. }
  2911. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  2912. struct perf_mmap_event *mmap_event)
  2913. {
  2914. struct perf_event *event;
  2915. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2916. if (perf_event_mmap_match(event, mmap_event))
  2917. perf_event_mmap_output(event, mmap_event);
  2918. }
  2919. }
  2920. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  2921. {
  2922. struct perf_cpu_context *cpuctx;
  2923. struct perf_event_context *ctx;
  2924. struct vm_area_struct *vma = mmap_event->vma;
  2925. struct file *file = vma->vm_file;
  2926. unsigned int size;
  2927. char tmp[16];
  2928. char *buf = NULL;
  2929. const char *name;
  2930. memset(tmp, 0, sizeof(tmp));
  2931. if (file) {
  2932. /*
  2933. * d_path works from the end of the buffer backwards, so we
  2934. * need to add enough zero bytes after the string to handle
  2935. * the 64bit alignment we do later.
  2936. */
  2937. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  2938. if (!buf) {
  2939. name = strncpy(tmp, "//enomem", sizeof(tmp));
  2940. goto got_name;
  2941. }
  2942. name = d_path(&file->f_path, buf, PATH_MAX);
  2943. if (IS_ERR(name)) {
  2944. name = strncpy(tmp, "//toolong", sizeof(tmp));
  2945. goto got_name;
  2946. }
  2947. } else {
  2948. if (arch_vma_name(mmap_event->vma)) {
  2949. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  2950. sizeof(tmp));
  2951. goto got_name;
  2952. }
  2953. if (!vma->vm_mm) {
  2954. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  2955. goto got_name;
  2956. }
  2957. name = strncpy(tmp, "//anon", sizeof(tmp));
  2958. goto got_name;
  2959. }
  2960. got_name:
  2961. size = ALIGN(strlen(name)+1, sizeof(u64));
  2962. mmap_event->file_name = name;
  2963. mmap_event->file_size = size;
  2964. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  2965. rcu_read_lock();
  2966. cpuctx = &get_cpu_var(perf_cpu_context);
  2967. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
  2968. ctx = rcu_dereference(current->perf_event_ctxp);
  2969. if (ctx)
  2970. perf_event_mmap_ctx(ctx, mmap_event);
  2971. put_cpu_var(perf_cpu_context);
  2972. rcu_read_unlock();
  2973. kfree(buf);
  2974. }
  2975. void __perf_event_mmap(struct vm_area_struct *vma)
  2976. {
  2977. struct perf_mmap_event mmap_event;
  2978. if (!atomic_read(&nr_mmap_events))
  2979. return;
  2980. mmap_event = (struct perf_mmap_event){
  2981. .vma = vma,
  2982. /* .file_name */
  2983. /* .file_size */
  2984. .event_id = {
  2985. .header = {
  2986. .type = PERF_RECORD_MMAP,
  2987. .misc = 0,
  2988. /* .size */
  2989. },
  2990. /* .pid */
  2991. /* .tid */
  2992. .start = vma->vm_start,
  2993. .len = vma->vm_end - vma->vm_start,
  2994. .pgoff = vma->vm_pgoff,
  2995. },
  2996. };
  2997. perf_event_mmap_event(&mmap_event);
  2998. }
  2999. /*
  3000. * IRQ throttle logging
  3001. */
  3002. static void perf_log_throttle(struct perf_event *event, int enable)
  3003. {
  3004. struct perf_output_handle handle;
  3005. int ret;
  3006. struct {
  3007. struct perf_event_header header;
  3008. u64 time;
  3009. u64 id;
  3010. u64 stream_id;
  3011. } throttle_event = {
  3012. .header = {
  3013. .type = PERF_RECORD_THROTTLE,
  3014. .misc = 0,
  3015. .size = sizeof(throttle_event),
  3016. },
  3017. .time = perf_clock(),
  3018. .id = primary_event_id(event),
  3019. .stream_id = event->id,
  3020. };
  3021. if (enable)
  3022. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3023. ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
  3024. if (ret)
  3025. return;
  3026. perf_output_put(&handle, throttle_event);
  3027. perf_output_end(&handle);
  3028. }
  3029. /*
  3030. * Generic event overflow handling, sampling.
  3031. */
  3032. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3033. int throttle, struct perf_sample_data *data,
  3034. struct pt_regs *regs)
  3035. {
  3036. int events = atomic_read(&event->event_limit);
  3037. struct hw_perf_event *hwc = &event->hw;
  3038. int ret = 0;
  3039. throttle = (throttle && event->pmu->unthrottle != NULL);
  3040. if (!throttle) {
  3041. hwc->interrupts++;
  3042. } else {
  3043. if (hwc->interrupts != MAX_INTERRUPTS) {
  3044. hwc->interrupts++;
  3045. if (HZ * hwc->interrupts >
  3046. (u64)sysctl_perf_event_sample_rate) {
  3047. hwc->interrupts = MAX_INTERRUPTS;
  3048. perf_log_throttle(event, 0);
  3049. ret = 1;
  3050. }
  3051. } else {
  3052. /*
  3053. * Keep re-disabling events even though on the previous
  3054. * pass we disabled it - just in case we raced with a
  3055. * sched-in and the event got enabled again:
  3056. */
  3057. ret = 1;
  3058. }
  3059. }
  3060. if (event->attr.freq) {
  3061. u64 now = perf_clock();
  3062. s64 delta = now - hwc->freq_stamp;
  3063. hwc->freq_stamp = now;
  3064. if (delta > 0 && delta < TICK_NSEC)
  3065. perf_adjust_period(event, NSEC_PER_SEC / (int)delta);
  3066. }
  3067. /*
  3068. * XXX event_limit might not quite work as expected on inherited
  3069. * events
  3070. */
  3071. event->pending_kill = POLL_IN;
  3072. if (events && atomic_dec_and_test(&event->event_limit)) {
  3073. ret = 1;
  3074. event->pending_kill = POLL_HUP;
  3075. if (nmi) {
  3076. event->pending_disable = 1;
  3077. perf_pending_queue(&event->pending,
  3078. perf_pending_event);
  3079. } else
  3080. perf_event_disable(event);
  3081. }
  3082. if (event->overflow_handler)
  3083. event->overflow_handler(event, nmi, data, regs);
  3084. else
  3085. perf_event_output(event, nmi, data, regs);
  3086. return ret;
  3087. }
  3088. int perf_event_overflow(struct perf_event *event, int nmi,
  3089. struct perf_sample_data *data,
  3090. struct pt_regs *regs)
  3091. {
  3092. return __perf_event_overflow(event, nmi, 1, data, regs);
  3093. }
  3094. /*
  3095. * Generic software event infrastructure
  3096. */
  3097. /*
  3098. * We directly increment event->count and keep a second value in
  3099. * event->hw.period_left to count intervals. This period event
  3100. * is kept in the range [-sample_period, 0] so that we can use the
  3101. * sign as trigger.
  3102. */
  3103. static u64 perf_swevent_set_period(struct perf_event *event)
  3104. {
  3105. struct hw_perf_event *hwc = &event->hw;
  3106. u64 period = hwc->last_period;
  3107. u64 nr, offset;
  3108. s64 old, val;
  3109. hwc->last_period = hwc->sample_period;
  3110. again:
  3111. old = val = atomic64_read(&hwc->period_left);
  3112. if (val < 0)
  3113. return 0;
  3114. nr = div64_u64(period + val, period);
  3115. offset = nr * period;
  3116. val -= offset;
  3117. if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
  3118. goto again;
  3119. return nr;
  3120. }
  3121. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3122. int nmi, struct perf_sample_data *data,
  3123. struct pt_regs *regs)
  3124. {
  3125. struct hw_perf_event *hwc = &event->hw;
  3126. int throttle = 0;
  3127. data->period = event->hw.last_period;
  3128. if (!overflow)
  3129. overflow = perf_swevent_set_period(event);
  3130. if (hwc->interrupts == MAX_INTERRUPTS)
  3131. return;
  3132. for (; overflow; overflow--) {
  3133. if (__perf_event_overflow(event, nmi, throttle,
  3134. data, regs)) {
  3135. /*
  3136. * We inhibit the overflow from happening when
  3137. * hwc->interrupts == MAX_INTERRUPTS.
  3138. */
  3139. break;
  3140. }
  3141. throttle = 1;
  3142. }
  3143. }
  3144. static void perf_swevent_unthrottle(struct perf_event *event)
  3145. {
  3146. /*
  3147. * Nothing to do, we already reset hwc->interrupts.
  3148. */
  3149. }
  3150. static void perf_swevent_add(struct perf_event *event, u64 nr,
  3151. int nmi, struct perf_sample_data *data,
  3152. struct pt_regs *regs)
  3153. {
  3154. struct hw_perf_event *hwc = &event->hw;
  3155. atomic64_add(nr, &event->count);
  3156. if (!regs)
  3157. return;
  3158. if (!hwc->sample_period)
  3159. return;
  3160. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3161. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3162. if (atomic64_add_negative(nr, &hwc->period_left))
  3163. return;
  3164. perf_swevent_overflow(event, 0, nmi, data, regs);
  3165. }
  3166. static int perf_swevent_is_counting(struct perf_event *event)
  3167. {
  3168. /*
  3169. * The event is active, we're good!
  3170. */
  3171. if (event->state == PERF_EVENT_STATE_ACTIVE)
  3172. return 1;
  3173. /*
  3174. * The event is off/error, not counting.
  3175. */
  3176. if (event->state != PERF_EVENT_STATE_INACTIVE)
  3177. return 0;
  3178. /*
  3179. * The event is inactive, if the context is active
  3180. * we're part of a group that didn't make it on the 'pmu',
  3181. * not counting.
  3182. */
  3183. if (event->ctx->is_active)
  3184. return 0;
  3185. /*
  3186. * We're inactive and the context is too, this means the
  3187. * task is scheduled out, we're counting events that happen
  3188. * to us, like migration events.
  3189. */
  3190. return 1;
  3191. }
  3192. static int perf_tp_event_match(struct perf_event *event,
  3193. struct perf_sample_data *data);
  3194. static int perf_exclude_event(struct perf_event *event,
  3195. struct pt_regs *regs)
  3196. {
  3197. if (regs) {
  3198. if (event->attr.exclude_user && user_mode(regs))
  3199. return 1;
  3200. if (event->attr.exclude_kernel && !user_mode(regs))
  3201. return 1;
  3202. }
  3203. return 0;
  3204. }
  3205. static int perf_swevent_match(struct perf_event *event,
  3206. enum perf_type_id type,
  3207. u32 event_id,
  3208. struct perf_sample_data *data,
  3209. struct pt_regs *regs)
  3210. {
  3211. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3212. return 0;
  3213. if (!perf_swevent_is_counting(event))
  3214. return 0;
  3215. if (event->attr.type != type)
  3216. return 0;
  3217. if (event->attr.config != event_id)
  3218. return 0;
  3219. if (perf_exclude_event(event, regs))
  3220. return 0;
  3221. if (event->attr.type == PERF_TYPE_TRACEPOINT &&
  3222. !perf_tp_event_match(event, data))
  3223. return 0;
  3224. return 1;
  3225. }
  3226. static void perf_swevent_ctx_event(struct perf_event_context *ctx,
  3227. enum perf_type_id type,
  3228. u32 event_id, u64 nr, int nmi,
  3229. struct perf_sample_data *data,
  3230. struct pt_regs *regs)
  3231. {
  3232. struct perf_event *event;
  3233. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3234. if (perf_swevent_match(event, type, event_id, data, regs))
  3235. perf_swevent_add(event, nr, nmi, data, regs);
  3236. }
  3237. }
  3238. int perf_swevent_get_recursion_context(void)
  3239. {
  3240. struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
  3241. int rctx;
  3242. if (in_nmi())
  3243. rctx = 3;
  3244. else if (in_irq())
  3245. rctx = 2;
  3246. else if (in_softirq())
  3247. rctx = 1;
  3248. else
  3249. rctx = 0;
  3250. if (cpuctx->recursion[rctx]) {
  3251. put_cpu_var(perf_cpu_context);
  3252. return -1;
  3253. }
  3254. cpuctx->recursion[rctx]++;
  3255. barrier();
  3256. return rctx;
  3257. }
  3258. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3259. void perf_swevent_put_recursion_context(int rctx)
  3260. {
  3261. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  3262. barrier();
  3263. cpuctx->recursion[rctx]--;
  3264. put_cpu_var(perf_cpu_context);
  3265. }
  3266. EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
  3267. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3268. u64 nr, int nmi,
  3269. struct perf_sample_data *data,
  3270. struct pt_regs *regs)
  3271. {
  3272. struct perf_cpu_context *cpuctx;
  3273. struct perf_event_context *ctx;
  3274. cpuctx = &__get_cpu_var(perf_cpu_context);
  3275. rcu_read_lock();
  3276. perf_swevent_ctx_event(&cpuctx->ctx, type, event_id,
  3277. nr, nmi, data, regs);
  3278. /*
  3279. * doesn't really matter which of the child contexts the
  3280. * events ends up in.
  3281. */
  3282. ctx = rcu_dereference(current->perf_event_ctxp);
  3283. if (ctx)
  3284. perf_swevent_ctx_event(ctx, type, event_id, nr, nmi, data, regs);
  3285. rcu_read_unlock();
  3286. }
  3287. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3288. struct pt_regs *regs, u64 addr)
  3289. {
  3290. struct perf_sample_data data;
  3291. int rctx;
  3292. rctx = perf_swevent_get_recursion_context();
  3293. if (rctx < 0)
  3294. return;
  3295. data.addr = addr;
  3296. data.raw = NULL;
  3297. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3298. perf_swevent_put_recursion_context(rctx);
  3299. }
  3300. static void perf_swevent_read(struct perf_event *event)
  3301. {
  3302. }
  3303. static int perf_swevent_enable(struct perf_event *event)
  3304. {
  3305. struct hw_perf_event *hwc = &event->hw;
  3306. if (hwc->sample_period) {
  3307. hwc->last_period = hwc->sample_period;
  3308. perf_swevent_set_period(event);
  3309. }
  3310. return 0;
  3311. }
  3312. static void perf_swevent_disable(struct perf_event *event)
  3313. {
  3314. }
  3315. static const struct pmu perf_ops_generic = {
  3316. .enable = perf_swevent_enable,
  3317. .disable = perf_swevent_disable,
  3318. .read = perf_swevent_read,
  3319. .unthrottle = perf_swevent_unthrottle,
  3320. };
  3321. /*
  3322. * hrtimer based swevent callback
  3323. */
  3324. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  3325. {
  3326. enum hrtimer_restart ret = HRTIMER_RESTART;
  3327. struct perf_sample_data data;
  3328. struct pt_regs *regs;
  3329. struct perf_event *event;
  3330. u64 period;
  3331. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  3332. event->pmu->read(event);
  3333. data.addr = 0;
  3334. data.raw = NULL;
  3335. data.period = event->hw.last_period;
  3336. regs = get_irq_regs();
  3337. /*
  3338. * In case we exclude kernel IPs or are somehow not in interrupt
  3339. * context, provide the next best thing, the user IP.
  3340. */
  3341. if ((event->attr.exclude_kernel || !regs) &&
  3342. !event->attr.exclude_user)
  3343. regs = task_pt_regs(current);
  3344. if (regs) {
  3345. if (!(event->attr.exclude_idle && current->pid == 0))
  3346. if (perf_event_overflow(event, 0, &data, regs))
  3347. ret = HRTIMER_NORESTART;
  3348. }
  3349. period = max_t(u64, 10000, event->hw.sample_period);
  3350. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  3351. return ret;
  3352. }
  3353. static void perf_swevent_start_hrtimer(struct perf_event *event)
  3354. {
  3355. struct hw_perf_event *hwc = &event->hw;
  3356. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3357. hwc->hrtimer.function = perf_swevent_hrtimer;
  3358. if (hwc->sample_period) {
  3359. u64 period;
  3360. if (hwc->remaining) {
  3361. if (hwc->remaining < 0)
  3362. period = 10000;
  3363. else
  3364. period = hwc->remaining;
  3365. hwc->remaining = 0;
  3366. } else {
  3367. period = max_t(u64, 10000, hwc->sample_period);
  3368. }
  3369. __hrtimer_start_range_ns(&hwc->hrtimer,
  3370. ns_to_ktime(period), 0,
  3371. HRTIMER_MODE_REL, 0);
  3372. }
  3373. }
  3374. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  3375. {
  3376. struct hw_perf_event *hwc = &event->hw;
  3377. if (hwc->sample_period) {
  3378. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  3379. hwc->remaining = ktime_to_ns(remaining);
  3380. hrtimer_cancel(&hwc->hrtimer);
  3381. }
  3382. }
  3383. /*
  3384. * Software event: cpu wall time clock
  3385. */
  3386. static void cpu_clock_perf_event_update(struct perf_event *event)
  3387. {
  3388. int cpu = raw_smp_processor_id();
  3389. s64 prev;
  3390. u64 now;
  3391. now = cpu_clock(cpu);
  3392. prev = atomic64_xchg(&event->hw.prev_count, now);
  3393. atomic64_add(now - prev, &event->count);
  3394. }
  3395. static int cpu_clock_perf_event_enable(struct perf_event *event)
  3396. {
  3397. struct hw_perf_event *hwc = &event->hw;
  3398. int cpu = raw_smp_processor_id();
  3399. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  3400. perf_swevent_start_hrtimer(event);
  3401. return 0;
  3402. }
  3403. static void cpu_clock_perf_event_disable(struct perf_event *event)
  3404. {
  3405. perf_swevent_cancel_hrtimer(event);
  3406. cpu_clock_perf_event_update(event);
  3407. }
  3408. static void cpu_clock_perf_event_read(struct perf_event *event)
  3409. {
  3410. cpu_clock_perf_event_update(event);
  3411. }
  3412. static const struct pmu perf_ops_cpu_clock = {
  3413. .enable = cpu_clock_perf_event_enable,
  3414. .disable = cpu_clock_perf_event_disable,
  3415. .read = cpu_clock_perf_event_read,
  3416. };
  3417. /*
  3418. * Software event: task time clock
  3419. */
  3420. static void task_clock_perf_event_update(struct perf_event *event, u64 now)
  3421. {
  3422. u64 prev;
  3423. s64 delta;
  3424. prev = atomic64_xchg(&event->hw.prev_count, now);
  3425. delta = now - prev;
  3426. atomic64_add(delta, &event->count);
  3427. }
  3428. static int task_clock_perf_event_enable(struct perf_event *event)
  3429. {
  3430. struct hw_perf_event *hwc = &event->hw;
  3431. u64 now;
  3432. now = event->ctx->time;
  3433. atomic64_set(&hwc->prev_count, now);
  3434. perf_swevent_start_hrtimer(event);
  3435. return 0;
  3436. }
  3437. static void task_clock_perf_event_disable(struct perf_event *event)
  3438. {
  3439. perf_swevent_cancel_hrtimer(event);
  3440. task_clock_perf_event_update(event, event->ctx->time);
  3441. }
  3442. static void task_clock_perf_event_read(struct perf_event *event)
  3443. {
  3444. u64 time;
  3445. if (!in_nmi()) {
  3446. update_context_time(event->ctx);
  3447. time = event->ctx->time;
  3448. } else {
  3449. u64 now = perf_clock();
  3450. u64 delta = now - event->ctx->timestamp;
  3451. time = event->ctx->time + delta;
  3452. }
  3453. task_clock_perf_event_update(event, time);
  3454. }
  3455. static const struct pmu perf_ops_task_clock = {
  3456. .enable = task_clock_perf_event_enable,
  3457. .disable = task_clock_perf_event_disable,
  3458. .read = task_clock_perf_event_read,
  3459. };
  3460. #ifdef CONFIG_EVENT_TRACING
  3461. void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
  3462. int entry_size)
  3463. {
  3464. struct perf_raw_record raw = {
  3465. .size = entry_size,
  3466. .data = record,
  3467. };
  3468. struct perf_sample_data data = {
  3469. .addr = addr,
  3470. .raw = &raw,
  3471. };
  3472. struct pt_regs *regs = get_irq_regs();
  3473. if (!regs)
  3474. regs = task_pt_regs(current);
  3475. /* Trace events already protected against recursion */
  3476. do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
  3477. &data, regs);
  3478. }
  3479. EXPORT_SYMBOL_GPL(perf_tp_event);
  3480. static int perf_tp_event_match(struct perf_event *event,
  3481. struct perf_sample_data *data)
  3482. {
  3483. void *record = data->raw->data;
  3484. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3485. return 1;
  3486. return 0;
  3487. }
  3488. static void tp_perf_event_destroy(struct perf_event *event)
  3489. {
  3490. ftrace_profile_disable(event->attr.config);
  3491. }
  3492. static const struct pmu *tp_perf_event_init(struct perf_event *event)
  3493. {
  3494. /*
  3495. * Raw tracepoint data is a severe data leak, only allow root to
  3496. * have these.
  3497. */
  3498. if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
  3499. perf_paranoid_tracepoint_raw() &&
  3500. !capable(CAP_SYS_ADMIN))
  3501. return ERR_PTR(-EPERM);
  3502. if (ftrace_profile_enable(event->attr.config))
  3503. return NULL;
  3504. event->destroy = tp_perf_event_destroy;
  3505. return &perf_ops_generic;
  3506. }
  3507. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3508. {
  3509. char *filter_str;
  3510. int ret;
  3511. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3512. return -EINVAL;
  3513. filter_str = strndup_user(arg, PAGE_SIZE);
  3514. if (IS_ERR(filter_str))
  3515. return PTR_ERR(filter_str);
  3516. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  3517. kfree(filter_str);
  3518. return ret;
  3519. }
  3520. static void perf_event_free_filter(struct perf_event *event)
  3521. {
  3522. ftrace_profile_free_filter(event);
  3523. }
  3524. #else
  3525. static int perf_tp_event_match(struct perf_event *event,
  3526. struct perf_sample_data *data)
  3527. {
  3528. return 1;
  3529. }
  3530. static const struct pmu *tp_perf_event_init(struct perf_event *event)
  3531. {
  3532. return NULL;
  3533. }
  3534. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3535. {
  3536. return -ENOENT;
  3537. }
  3538. static void perf_event_free_filter(struct perf_event *event)
  3539. {
  3540. }
  3541. #endif /* CONFIG_EVENT_TRACING */
  3542. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  3543. static void bp_perf_event_destroy(struct perf_event *event)
  3544. {
  3545. release_bp_slot(event);
  3546. }
  3547. static const struct pmu *bp_perf_event_init(struct perf_event *bp)
  3548. {
  3549. int err;
  3550. err = register_perf_hw_breakpoint(bp);
  3551. if (err)
  3552. return ERR_PTR(err);
  3553. bp->destroy = bp_perf_event_destroy;
  3554. return &perf_ops_bp;
  3555. }
  3556. void perf_bp_event(struct perf_event *bp, void *data)
  3557. {
  3558. struct perf_sample_data sample;
  3559. struct pt_regs *regs = data;
  3560. sample.raw = NULL;
  3561. sample.addr = bp->attr.bp_addr;
  3562. if (!perf_exclude_event(bp, regs))
  3563. perf_swevent_add(bp, 1, 1, &sample, regs);
  3564. }
  3565. #else
  3566. static const struct pmu *bp_perf_event_init(struct perf_event *bp)
  3567. {
  3568. return NULL;
  3569. }
  3570. void perf_bp_event(struct perf_event *bp, void *regs)
  3571. {
  3572. }
  3573. #endif
  3574. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3575. static void sw_perf_event_destroy(struct perf_event *event)
  3576. {
  3577. u64 event_id = event->attr.config;
  3578. WARN_ON(event->parent);
  3579. atomic_dec(&perf_swevent_enabled[event_id]);
  3580. }
  3581. static const struct pmu *sw_perf_event_init(struct perf_event *event)
  3582. {
  3583. const struct pmu *pmu = NULL;
  3584. u64 event_id = event->attr.config;
  3585. /*
  3586. * Software events (currently) can't in general distinguish
  3587. * between user, kernel and hypervisor events.
  3588. * However, context switches and cpu migrations are considered
  3589. * to be kernel events, and page faults are never hypervisor
  3590. * events.
  3591. */
  3592. switch (event_id) {
  3593. case PERF_COUNT_SW_CPU_CLOCK:
  3594. pmu = &perf_ops_cpu_clock;
  3595. break;
  3596. case PERF_COUNT_SW_TASK_CLOCK:
  3597. /*
  3598. * If the user instantiates this as a per-cpu event,
  3599. * use the cpu_clock event instead.
  3600. */
  3601. if (event->ctx->task)
  3602. pmu = &perf_ops_task_clock;
  3603. else
  3604. pmu = &perf_ops_cpu_clock;
  3605. break;
  3606. case PERF_COUNT_SW_PAGE_FAULTS:
  3607. case PERF_COUNT_SW_PAGE_FAULTS_MIN:
  3608. case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
  3609. case PERF_COUNT_SW_CONTEXT_SWITCHES:
  3610. case PERF_COUNT_SW_CPU_MIGRATIONS:
  3611. case PERF_COUNT_SW_ALIGNMENT_FAULTS:
  3612. case PERF_COUNT_SW_EMULATION_FAULTS:
  3613. if (!event->parent) {
  3614. atomic_inc(&perf_swevent_enabled[event_id]);
  3615. event->destroy = sw_perf_event_destroy;
  3616. }
  3617. pmu = &perf_ops_generic;
  3618. break;
  3619. }
  3620. return pmu;
  3621. }
  3622. /*
  3623. * Allocate and initialize a event structure
  3624. */
  3625. static struct perf_event *
  3626. perf_event_alloc(struct perf_event_attr *attr,
  3627. int cpu,
  3628. struct perf_event_context *ctx,
  3629. struct perf_event *group_leader,
  3630. struct perf_event *parent_event,
  3631. perf_overflow_handler_t overflow_handler,
  3632. gfp_t gfpflags)
  3633. {
  3634. const struct pmu *pmu;
  3635. struct perf_event *event;
  3636. struct hw_perf_event *hwc;
  3637. long err;
  3638. event = kzalloc(sizeof(*event), gfpflags);
  3639. if (!event)
  3640. return ERR_PTR(-ENOMEM);
  3641. /*
  3642. * Single events are their own group leaders, with an
  3643. * empty sibling list:
  3644. */
  3645. if (!group_leader)
  3646. group_leader = event;
  3647. mutex_init(&event->child_mutex);
  3648. INIT_LIST_HEAD(&event->child_list);
  3649. INIT_LIST_HEAD(&event->group_entry);
  3650. INIT_LIST_HEAD(&event->event_entry);
  3651. INIT_LIST_HEAD(&event->sibling_list);
  3652. init_waitqueue_head(&event->waitq);
  3653. mutex_init(&event->mmap_mutex);
  3654. event->cpu = cpu;
  3655. event->attr = *attr;
  3656. event->group_leader = group_leader;
  3657. event->pmu = NULL;
  3658. event->ctx = ctx;
  3659. event->oncpu = -1;
  3660. event->parent = parent_event;
  3661. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  3662. event->id = atomic64_inc_return(&perf_event_id);
  3663. event->state = PERF_EVENT_STATE_INACTIVE;
  3664. if (!overflow_handler && parent_event)
  3665. overflow_handler = parent_event->overflow_handler;
  3666. event->overflow_handler = overflow_handler;
  3667. if (attr->disabled)
  3668. event->state = PERF_EVENT_STATE_OFF;
  3669. pmu = NULL;
  3670. hwc = &event->hw;
  3671. hwc->sample_period = attr->sample_period;
  3672. if (attr->freq && attr->sample_freq)
  3673. hwc->sample_period = 1;
  3674. hwc->last_period = hwc->sample_period;
  3675. atomic64_set(&hwc->period_left, hwc->sample_period);
  3676. /*
  3677. * we currently do not support PERF_FORMAT_GROUP on inherited events
  3678. */
  3679. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  3680. goto done;
  3681. switch (attr->type) {
  3682. case PERF_TYPE_RAW:
  3683. case PERF_TYPE_HARDWARE:
  3684. case PERF_TYPE_HW_CACHE:
  3685. pmu = hw_perf_event_init(event);
  3686. break;
  3687. case PERF_TYPE_SOFTWARE:
  3688. pmu = sw_perf_event_init(event);
  3689. break;
  3690. case PERF_TYPE_TRACEPOINT:
  3691. pmu = tp_perf_event_init(event);
  3692. break;
  3693. case PERF_TYPE_BREAKPOINT:
  3694. pmu = bp_perf_event_init(event);
  3695. break;
  3696. default:
  3697. break;
  3698. }
  3699. done:
  3700. err = 0;
  3701. if (!pmu)
  3702. err = -EINVAL;
  3703. else if (IS_ERR(pmu))
  3704. err = PTR_ERR(pmu);
  3705. if (err) {
  3706. if (event->ns)
  3707. put_pid_ns(event->ns);
  3708. kfree(event);
  3709. return ERR_PTR(err);
  3710. }
  3711. event->pmu = pmu;
  3712. if (!event->parent) {
  3713. atomic_inc(&nr_events);
  3714. if (event->attr.mmap)
  3715. atomic_inc(&nr_mmap_events);
  3716. if (event->attr.comm)
  3717. atomic_inc(&nr_comm_events);
  3718. if (event->attr.task)
  3719. atomic_inc(&nr_task_events);
  3720. }
  3721. return event;
  3722. }
  3723. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  3724. struct perf_event_attr *attr)
  3725. {
  3726. u32 size;
  3727. int ret;
  3728. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  3729. return -EFAULT;
  3730. /*
  3731. * zero the full structure, so that a short copy will be nice.
  3732. */
  3733. memset(attr, 0, sizeof(*attr));
  3734. ret = get_user(size, &uattr->size);
  3735. if (ret)
  3736. return ret;
  3737. if (size > PAGE_SIZE) /* silly large */
  3738. goto err_size;
  3739. if (!size) /* abi compat */
  3740. size = PERF_ATTR_SIZE_VER0;
  3741. if (size < PERF_ATTR_SIZE_VER0)
  3742. goto err_size;
  3743. /*
  3744. * If we're handed a bigger struct than we know of,
  3745. * ensure all the unknown bits are 0 - i.e. new
  3746. * user-space does not rely on any kernel feature
  3747. * extensions we dont know about yet.
  3748. */
  3749. if (size > sizeof(*attr)) {
  3750. unsigned char __user *addr;
  3751. unsigned char __user *end;
  3752. unsigned char val;
  3753. addr = (void __user *)uattr + sizeof(*attr);
  3754. end = (void __user *)uattr + size;
  3755. for (; addr < end; addr++) {
  3756. ret = get_user(val, addr);
  3757. if (ret)
  3758. return ret;
  3759. if (val)
  3760. goto err_size;
  3761. }
  3762. size = sizeof(*attr);
  3763. }
  3764. ret = copy_from_user(attr, uattr, size);
  3765. if (ret)
  3766. return -EFAULT;
  3767. /*
  3768. * If the type exists, the corresponding creation will verify
  3769. * the attr->config.
  3770. */
  3771. if (attr->type >= PERF_TYPE_MAX)
  3772. return -EINVAL;
  3773. if (attr->__reserved_1 || attr->__reserved_2)
  3774. return -EINVAL;
  3775. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  3776. return -EINVAL;
  3777. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  3778. return -EINVAL;
  3779. out:
  3780. return ret;
  3781. err_size:
  3782. put_user(sizeof(*attr), &uattr->size);
  3783. ret = -E2BIG;
  3784. goto out;
  3785. }
  3786. static int perf_event_set_output(struct perf_event *event, int output_fd)
  3787. {
  3788. struct perf_event *output_event = NULL;
  3789. struct file *output_file = NULL;
  3790. struct perf_event *old_output;
  3791. int fput_needed = 0;
  3792. int ret = -EINVAL;
  3793. if (!output_fd)
  3794. goto set;
  3795. output_file = fget_light(output_fd, &fput_needed);
  3796. if (!output_file)
  3797. return -EBADF;
  3798. if (output_file->f_op != &perf_fops)
  3799. goto out;
  3800. output_event = output_file->private_data;
  3801. /* Don't chain output fds */
  3802. if (output_event->output)
  3803. goto out;
  3804. /* Don't set an output fd when we already have an output channel */
  3805. if (event->data)
  3806. goto out;
  3807. atomic_long_inc(&output_file->f_count);
  3808. set:
  3809. mutex_lock(&event->mmap_mutex);
  3810. old_output = event->output;
  3811. rcu_assign_pointer(event->output, output_event);
  3812. mutex_unlock(&event->mmap_mutex);
  3813. if (old_output) {
  3814. /*
  3815. * we need to make sure no existing perf_output_*()
  3816. * is still referencing this event.
  3817. */
  3818. synchronize_rcu();
  3819. fput(old_output->filp);
  3820. }
  3821. ret = 0;
  3822. out:
  3823. fput_light(output_file, fput_needed);
  3824. return ret;
  3825. }
  3826. /**
  3827. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  3828. *
  3829. * @attr_uptr: event_id type attributes for monitoring/sampling
  3830. * @pid: target pid
  3831. * @cpu: target cpu
  3832. * @group_fd: group leader event fd
  3833. */
  3834. SYSCALL_DEFINE5(perf_event_open,
  3835. struct perf_event_attr __user *, attr_uptr,
  3836. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  3837. {
  3838. struct perf_event *event, *group_leader;
  3839. struct perf_event_attr attr;
  3840. struct perf_event_context *ctx;
  3841. struct file *event_file = NULL;
  3842. struct file *group_file = NULL;
  3843. int fput_needed = 0;
  3844. int fput_needed2 = 0;
  3845. int err;
  3846. /* for future expandability... */
  3847. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  3848. return -EINVAL;
  3849. err = perf_copy_attr(attr_uptr, &attr);
  3850. if (err)
  3851. return err;
  3852. if (!attr.exclude_kernel) {
  3853. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  3854. return -EACCES;
  3855. }
  3856. if (attr.freq) {
  3857. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  3858. return -EINVAL;
  3859. }
  3860. /*
  3861. * Get the target context (task or percpu):
  3862. */
  3863. ctx = find_get_context(pid, cpu);
  3864. if (IS_ERR(ctx))
  3865. return PTR_ERR(ctx);
  3866. /*
  3867. * Look up the group leader (we will attach this event to it):
  3868. */
  3869. group_leader = NULL;
  3870. if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
  3871. err = -EINVAL;
  3872. group_file = fget_light(group_fd, &fput_needed);
  3873. if (!group_file)
  3874. goto err_put_context;
  3875. if (group_file->f_op != &perf_fops)
  3876. goto err_put_context;
  3877. group_leader = group_file->private_data;
  3878. /*
  3879. * Do not allow a recursive hierarchy (this new sibling
  3880. * becoming part of another group-sibling):
  3881. */
  3882. if (group_leader->group_leader != group_leader)
  3883. goto err_put_context;
  3884. /*
  3885. * Do not allow to attach to a group in a different
  3886. * task or CPU context:
  3887. */
  3888. if (group_leader->ctx != ctx)
  3889. goto err_put_context;
  3890. /*
  3891. * Only a group leader can be exclusive or pinned
  3892. */
  3893. if (attr.exclusive || attr.pinned)
  3894. goto err_put_context;
  3895. }
  3896. event = perf_event_alloc(&attr, cpu, ctx, group_leader,
  3897. NULL, NULL, GFP_KERNEL);
  3898. err = PTR_ERR(event);
  3899. if (IS_ERR(event))
  3900. goto err_put_context;
  3901. err = anon_inode_getfd("[perf_event]", &perf_fops, event, O_RDWR);
  3902. if (err < 0)
  3903. goto err_free_put_context;
  3904. event_file = fget_light(err, &fput_needed2);
  3905. if (!event_file)
  3906. goto err_free_put_context;
  3907. if (flags & PERF_FLAG_FD_OUTPUT) {
  3908. err = perf_event_set_output(event, group_fd);
  3909. if (err)
  3910. goto err_fput_free_put_context;
  3911. }
  3912. event->filp = event_file;
  3913. WARN_ON_ONCE(ctx->parent_ctx);
  3914. mutex_lock(&ctx->mutex);
  3915. perf_install_in_context(ctx, event, cpu);
  3916. ++ctx->generation;
  3917. mutex_unlock(&ctx->mutex);
  3918. event->owner = current;
  3919. get_task_struct(current);
  3920. mutex_lock(&current->perf_event_mutex);
  3921. list_add_tail(&event->owner_entry, &current->perf_event_list);
  3922. mutex_unlock(&current->perf_event_mutex);
  3923. err_fput_free_put_context:
  3924. fput_light(event_file, fput_needed2);
  3925. err_free_put_context:
  3926. if (err < 0)
  3927. kfree(event);
  3928. err_put_context:
  3929. if (err < 0)
  3930. put_ctx(ctx);
  3931. fput_light(group_file, fput_needed);
  3932. return err;
  3933. }
  3934. /**
  3935. * perf_event_create_kernel_counter
  3936. *
  3937. * @attr: attributes of the counter to create
  3938. * @cpu: cpu in which the counter is bound
  3939. * @pid: task to profile
  3940. */
  3941. struct perf_event *
  3942. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  3943. pid_t pid,
  3944. perf_overflow_handler_t overflow_handler)
  3945. {
  3946. struct perf_event *event;
  3947. struct perf_event_context *ctx;
  3948. int err;
  3949. /*
  3950. * Get the target context (task or percpu):
  3951. */
  3952. ctx = find_get_context(pid, cpu);
  3953. if (IS_ERR(ctx)) {
  3954. err = PTR_ERR(ctx);
  3955. goto err_exit;
  3956. }
  3957. event = perf_event_alloc(attr, cpu, ctx, NULL,
  3958. NULL, overflow_handler, GFP_KERNEL);
  3959. if (IS_ERR(event)) {
  3960. err = PTR_ERR(event);
  3961. goto err_put_context;
  3962. }
  3963. event->filp = NULL;
  3964. WARN_ON_ONCE(ctx->parent_ctx);
  3965. mutex_lock(&ctx->mutex);
  3966. perf_install_in_context(ctx, event, cpu);
  3967. ++ctx->generation;
  3968. mutex_unlock(&ctx->mutex);
  3969. event->owner = current;
  3970. get_task_struct(current);
  3971. mutex_lock(&current->perf_event_mutex);
  3972. list_add_tail(&event->owner_entry, &current->perf_event_list);
  3973. mutex_unlock(&current->perf_event_mutex);
  3974. return event;
  3975. err_put_context:
  3976. put_ctx(ctx);
  3977. err_exit:
  3978. return ERR_PTR(err);
  3979. }
  3980. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  3981. /*
  3982. * inherit a event from parent task to child task:
  3983. */
  3984. static struct perf_event *
  3985. inherit_event(struct perf_event *parent_event,
  3986. struct task_struct *parent,
  3987. struct perf_event_context *parent_ctx,
  3988. struct task_struct *child,
  3989. struct perf_event *group_leader,
  3990. struct perf_event_context *child_ctx)
  3991. {
  3992. struct perf_event *child_event;
  3993. /*
  3994. * Instead of creating recursive hierarchies of events,
  3995. * we link inherited events back to the original parent,
  3996. * which has a filp for sure, which we use as the reference
  3997. * count:
  3998. */
  3999. if (parent_event->parent)
  4000. parent_event = parent_event->parent;
  4001. child_event = perf_event_alloc(&parent_event->attr,
  4002. parent_event->cpu, child_ctx,
  4003. group_leader, parent_event,
  4004. NULL, GFP_KERNEL);
  4005. if (IS_ERR(child_event))
  4006. return child_event;
  4007. get_ctx(child_ctx);
  4008. /*
  4009. * Make the child state follow the state of the parent event,
  4010. * not its attr.disabled bit. We hold the parent's mutex,
  4011. * so we won't race with perf_event_{en, dis}able_family.
  4012. */
  4013. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  4014. child_event->state = PERF_EVENT_STATE_INACTIVE;
  4015. else
  4016. child_event->state = PERF_EVENT_STATE_OFF;
  4017. if (parent_event->attr.freq)
  4018. child_event->hw.sample_period = parent_event->hw.sample_period;
  4019. child_event->overflow_handler = parent_event->overflow_handler;
  4020. /*
  4021. * Link it up in the child's context:
  4022. */
  4023. add_event_to_ctx(child_event, child_ctx);
  4024. /*
  4025. * Get a reference to the parent filp - we will fput it
  4026. * when the child event exits. This is safe to do because
  4027. * we are in the parent and we know that the filp still
  4028. * exists and has a nonzero count:
  4029. */
  4030. atomic_long_inc(&parent_event->filp->f_count);
  4031. /*
  4032. * Link this into the parent event's child list
  4033. */
  4034. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4035. mutex_lock(&parent_event->child_mutex);
  4036. list_add_tail(&child_event->child_list, &parent_event->child_list);
  4037. mutex_unlock(&parent_event->child_mutex);
  4038. return child_event;
  4039. }
  4040. static int inherit_group(struct perf_event *parent_event,
  4041. struct task_struct *parent,
  4042. struct perf_event_context *parent_ctx,
  4043. struct task_struct *child,
  4044. struct perf_event_context *child_ctx)
  4045. {
  4046. struct perf_event *leader;
  4047. struct perf_event *sub;
  4048. struct perf_event *child_ctr;
  4049. leader = inherit_event(parent_event, parent, parent_ctx,
  4050. child, NULL, child_ctx);
  4051. if (IS_ERR(leader))
  4052. return PTR_ERR(leader);
  4053. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  4054. child_ctr = inherit_event(sub, parent, parent_ctx,
  4055. child, leader, child_ctx);
  4056. if (IS_ERR(child_ctr))
  4057. return PTR_ERR(child_ctr);
  4058. }
  4059. return 0;
  4060. }
  4061. static void sync_child_event(struct perf_event *child_event,
  4062. struct task_struct *child)
  4063. {
  4064. struct perf_event *parent_event = child_event->parent;
  4065. u64 child_val;
  4066. if (child_event->attr.inherit_stat)
  4067. perf_event_read_event(child_event, child);
  4068. child_val = atomic64_read(&child_event->count);
  4069. /*
  4070. * Add back the child's count to the parent's count:
  4071. */
  4072. atomic64_add(child_val, &parent_event->count);
  4073. atomic64_add(child_event->total_time_enabled,
  4074. &parent_event->child_total_time_enabled);
  4075. atomic64_add(child_event->total_time_running,
  4076. &parent_event->child_total_time_running);
  4077. /*
  4078. * Remove this event from the parent's list
  4079. */
  4080. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4081. mutex_lock(&parent_event->child_mutex);
  4082. list_del_init(&child_event->child_list);
  4083. mutex_unlock(&parent_event->child_mutex);
  4084. /*
  4085. * Release the parent event, if this was the last
  4086. * reference to it.
  4087. */
  4088. fput(parent_event->filp);
  4089. }
  4090. static void
  4091. __perf_event_exit_task(struct perf_event *child_event,
  4092. struct perf_event_context *child_ctx,
  4093. struct task_struct *child)
  4094. {
  4095. struct perf_event *parent_event;
  4096. perf_event_remove_from_context(child_event);
  4097. parent_event = child_event->parent;
  4098. /*
  4099. * It can happen that parent exits first, and has events
  4100. * that are still around due to the child reference. These
  4101. * events need to be zapped - but otherwise linger.
  4102. */
  4103. if (parent_event) {
  4104. sync_child_event(child_event, child);
  4105. free_event(child_event);
  4106. }
  4107. }
  4108. /*
  4109. * When a child task exits, feed back event values to parent events.
  4110. */
  4111. void perf_event_exit_task(struct task_struct *child)
  4112. {
  4113. struct perf_event *child_event, *tmp;
  4114. struct perf_event_context *child_ctx;
  4115. unsigned long flags;
  4116. if (likely(!child->perf_event_ctxp)) {
  4117. perf_event_task(child, NULL, 0);
  4118. return;
  4119. }
  4120. local_irq_save(flags);
  4121. /*
  4122. * We can't reschedule here because interrupts are disabled,
  4123. * and either child is current or it is a task that can't be
  4124. * scheduled, so we are now safe from rescheduling changing
  4125. * our context.
  4126. */
  4127. child_ctx = child->perf_event_ctxp;
  4128. __perf_event_task_sched_out(child_ctx);
  4129. /*
  4130. * Take the context lock here so that if find_get_context is
  4131. * reading child->perf_event_ctxp, we wait until it has
  4132. * incremented the context's refcount before we do put_ctx below.
  4133. */
  4134. raw_spin_lock(&child_ctx->lock);
  4135. child->perf_event_ctxp = NULL;
  4136. /*
  4137. * If this context is a clone; unclone it so it can't get
  4138. * swapped to another process while we're removing all
  4139. * the events from it.
  4140. */
  4141. unclone_ctx(child_ctx);
  4142. update_context_time(child_ctx);
  4143. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4144. /*
  4145. * Report the task dead after unscheduling the events so that we
  4146. * won't get any samples after PERF_RECORD_EXIT. We can however still
  4147. * get a few PERF_RECORD_READ events.
  4148. */
  4149. perf_event_task(child, child_ctx, 0);
  4150. /*
  4151. * We can recurse on the same lock type through:
  4152. *
  4153. * __perf_event_exit_task()
  4154. * sync_child_event()
  4155. * fput(parent_event->filp)
  4156. * perf_release()
  4157. * mutex_lock(&ctx->mutex)
  4158. *
  4159. * But since its the parent context it won't be the same instance.
  4160. */
  4161. mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
  4162. again:
  4163. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  4164. group_entry)
  4165. __perf_event_exit_task(child_event, child_ctx, child);
  4166. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  4167. group_entry)
  4168. __perf_event_exit_task(child_event, child_ctx, child);
  4169. /*
  4170. * If the last event was a group event, it will have appended all
  4171. * its siblings to the list, but we obtained 'tmp' before that which
  4172. * will still point to the list head terminating the iteration.
  4173. */
  4174. if (!list_empty(&child_ctx->pinned_groups) ||
  4175. !list_empty(&child_ctx->flexible_groups))
  4176. goto again;
  4177. mutex_unlock(&child_ctx->mutex);
  4178. put_ctx(child_ctx);
  4179. }
  4180. static void perf_free_event(struct perf_event *event,
  4181. struct perf_event_context *ctx)
  4182. {
  4183. struct perf_event *parent = event->parent;
  4184. if (WARN_ON_ONCE(!parent))
  4185. return;
  4186. mutex_lock(&parent->child_mutex);
  4187. list_del_init(&event->child_list);
  4188. mutex_unlock(&parent->child_mutex);
  4189. fput(parent->filp);
  4190. list_del_event(event, ctx);
  4191. free_event(event);
  4192. }
  4193. /*
  4194. * free an unexposed, unused context as created by inheritance by
  4195. * init_task below, used by fork() in case of fail.
  4196. */
  4197. void perf_event_free_task(struct task_struct *task)
  4198. {
  4199. struct perf_event_context *ctx = task->perf_event_ctxp;
  4200. struct perf_event *event, *tmp;
  4201. if (!ctx)
  4202. return;
  4203. mutex_lock(&ctx->mutex);
  4204. again:
  4205. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  4206. perf_free_event(event, ctx);
  4207. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  4208. group_entry)
  4209. perf_free_event(event, ctx);
  4210. if (!list_empty(&ctx->pinned_groups) ||
  4211. !list_empty(&ctx->flexible_groups))
  4212. goto again;
  4213. mutex_unlock(&ctx->mutex);
  4214. put_ctx(ctx);
  4215. }
  4216. static int
  4217. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  4218. struct perf_event_context *parent_ctx,
  4219. struct task_struct *child,
  4220. int *inherited_all)
  4221. {
  4222. int ret;
  4223. struct perf_event_context *child_ctx = child->perf_event_ctxp;
  4224. if (!event->attr.inherit) {
  4225. *inherited_all = 0;
  4226. return 0;
  4227. }
  4228. if (!child_ctx) {
  4229. /*
  4230. * This is executed from the parent task context, so
  4231. * inherit events that have been marked for cloning.
  4232. * First allocate and initialize a context for the
  4233. * child.
  4234. */
  4235. child_ctx = kzalloc(sizeof(struct perf_event_context),
  4236. GFP_KERNEL);
  4237. if (!child_ctx)
  4238. return -ENOMEM;
  4239. __perf_event_init_context(child_ctx, child);
  4240. child->perf_event_ctxp = child_ctx;
  4241. get_task_struct(child);
  4242. }
  4243. ret = inherit_group(event, parent, parent_ctx,
  4244. child, child_ctx);
  4245. if (ret)
  4246. *inherited_all = 0;
  4247. return ret;
  4248. }
  4249. /*
  4250. * Initialize the perf_event context in task_struct
  4251. */
  4252. int perf_event_init_task(struct task_struct *child)
  4253. {
  4254. struct perf_event_context *child_ctx, *parent_ctx;
  4255. struct perf_event_context *cloned_ctx;
  4256. struct perf_event *event;
  4257. struct task_struct *parent = current;
  4258. int inherited_all = 1;
  4259. int ret = 0;
  4260. child->perf_event_ctxp = NULL;
  4261. mutex_init(&child->perf_event_mutex);
  4262. INIT_LIST_HEAD(&child->perf_event_list);
  4263. if (likely(!parent->perf_event_ctxp))
  4264. return 0;
  4265. /*
  4266. * If the parent's context is a clone, pin it so it won't get
  4267. * swapped under us.
  4268. */
  4269. parent_ctx = perf_pin_task_context(parent);
  4270. /*
  4271. * No need to check if parent_ctx != NULL here; since we saw
  4272. * it non-NULL earlier, the only reason for it to become NULL
  4273. * is if we exit, and since we're currently in the middle of
  4274. * a fork we can't be exiting at the same time.
  4275. */
  4276. /*
  4277. * Lock the parent list. No need to lock the child - not PID
  4278. * hashed yet and not running, so nobody can access it.
  4279. */
  4280. mutex_lock(&parent_ctx->mutex);
  4281. /*
  4282. * We dont have to disable NMIs - we are only looking at
  4283. * the list, not manipulating it:
  4284. */
  4285. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  4286. ret = inherit_task_group(event, parent, parent_ctx, child,
  4287. &inherited_all);
  4288. if (ret)
  4289. break;
  4290. }
  4291. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  4292. ret = inherit_task_group(event, parent, parent_ctx, child,
  4293. &inherited_all);
  4294. if (ret)
  4295. break;
  4296. }
  4297. child_ctx = child->perf_event_ctxp;
  4298. if (child_ctx && inherited_all) {
  4299. /*
  4300. * Mark the child context as a clone of the parent
  4301. * context, or of whatever the parent is a clone of.
  4302. * Note that if the parent is a clone, it could get
  4303. * uncloned at any point, but that doesn't matter
  4304. * because the list of events and the generation
  4305. * count can't have changed since we took the mutex.
  4306. */
  4307. cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
  4308. if (cloned_ctx) {
  4309. child_ctx->parent_ctx = cloned_ctx;
  4310. child_ctx->parent_gen = parent_ctx->parent_gen;
  4311. } else {
  4312. child_ctx->parent_ctx = parent_ctx;
  4313. child_ctx->parent_gen = parent_ctx->generation;
  4314. }
  4315. get_ctx(child_ctx->parent_ctx);
  4316. }
  4317. mutex_unlock(&parent_ctx->mutex);
  4318. perf_unpin_context(parent_ctx);
  4319. return ret;
  4320. }
  4321. static void __cpuinit perf_event_init_cpu(int cpu)
  4322. {
  4323. struct perf_cpu_context *cpuctx;
  4324. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4325. __perf_event_init_context(&cpuctx->ctx, NULL);
  4326. spin_lock(&perf_resource_lock);
  4327. cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
  4328. spin_unlock(&perf_resource_lock);
  4329. hw_perf_event_setup(cpu);
  4330. }
  4331. #ifdef CONFIG_HOTPLUG_CPU
  4332. static void __perf_event_exit_cpu(void *info)
  4333. {
  4334. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  4335. struct perf_event_context *ctx = &cpuctx->ctx;
  4336. struct perf_event *event, *tmp;
  4337. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  4338. __perf_event_remove_from_context(event);
  4339. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  4340. __perf_event_remove_from_context(event);
  4341. }
  4342. static void perf_event_exit_cpu(int cpu)
  4343. {
  4344. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  4345. struct perf_event_context *ctx = &cpuctx->ctx;
  4346. mutex_lock(&ctx->mutex);
  4347. smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
  4348. mutex_unlock(&ctx->mutex);
  4349. }
  4350. #else
  4351. static inline void perf_event_exit_cpu(int cpu) { }
  4352. #endif
  4353. static int __cpuinit
  4354. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  4355. {
  4356. unsigned int cpu = (long)hcpu;
  4357. switch (action) {
  4358. case CPU_UP_PREPARE:
  4359. case CPU_UP_PREPARE_FROZEN:
  4360. perf_event_init_cpu(cpu);
  4361. break;
  4362. case CPU_ONLINE:
  4363. case CPU_ONLINE_FROZEN:
  4364. hw_perf_event_setup_online(cpu);
  4365. break;
  4366. case CPU_DOWN_PREPARE:
  4367. case CPU_DOWN_PREPARE_FROZEN:
  4368. perf_event_exit_cpu(cpu);
  4369. break;
  4370. default:
  4371. break;
  4372. }
  4373. return NOTIFY_OK;
  4374. }
  4375. /*
  4376. * This has to have a higher priority than migration_notifier in sched.c.
  4377. */
  4378. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  4379. .notifier_call = perf_cpu_notify,
  4380. .priority = 20,
  4381. };
  4382. void __init perf_event_init(void)
  4383. {
  4384. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  4385. (void *)(long)smp_processor_id());
  4386. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
  4387. (void *)(long)smp_processor_id());
  4388. register_cpu_notifier(&perf_cpu_nb);
  4389. }
  4390. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  4391. {
  4392. return sprintf(buf, "%d\n", perf_reserved_percpu);
  4393. }
  4394. static ssize_t
  4395. perf_set_reserve_percpu(struct sysdev_class *class,
  4396. const char *buf,
  4397. size_t count)
  4398. {
  4399. struct perf_cpu_context *cpuctx;
  4400. unsigned long val;
  4401. int err, cpu, mpt;
  4402. err = strict_strtoul(buf, 10, &val);
  4403. if (err)
  4404. return err;
  4405. if (val > perf_max_events)
  4406. return -EINVAL;
  4407. spin_lock(&perf_resource_lock);
  4408. perf_reserved_percpu = val;
  4409. for_each_online_cpu(cpu) {
  4410. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4411. raw_spin_lock_irq(&cpuctx->ctx.lock);
  4412. mpt = min(perf_max_events - cpuctx->ctx.nr_events,
  4413. perf_max_events - perf_reserved_percpu);
  4414. cpuctx->max_pertask = mpt;
  4415. raw_spin_unlock_irq(&cpuctx->ctx.lock);
  4416. }
  4417. spin_unlock(&perf_resource_lock);
  4418. return count;
  4419. }
  4420. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  4421. {
  4422. return sprintf(buf, "%d\n", perf_overcommit);
  4423. }
  4424. static ssize_t
  4425. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  4426. {
  4427. unsigned long val;
  4428. int err;
  4429. err = strict_strtoul(buf, 10, &val);
  4430. if (err)
  4431. return err;
  4432. if (val > 1)
  4433. return -EINVAL;
  4434. spin_lock(&perf_resource_lock);
  4435. perf_overcommit = val;
  4436. spin_unlock(&perf_resource_lock);
  4437. return count;
  4438. }
  4439. static SYSDEV_CLASS_ATTR(
  4440. reserve_percpu,
  4441. 0644,
  4442. perf_show_reserve_percpu,
  4443. perf_set_reserve_percpu
  4444. );
  4445. static SYSDEV_CLASS_ATTR(
  4446. overcommit,
  4447. 0644,
  4448. perf_show_overcommit,
  4449. perf_set_overcommit
  4450. );
  4451. static struct attribute *perfclass_attrs[] = {
  4452. &attr_reserve_percpu.attr,
  4453. &attr_overcommit.attr,
  4454. NULL
  4455. };
  4456. static struct attribute_group perfclass_attr_group = {
  4457. .attrs = perfclass_attrs,
  4458. .name = "perf_events",
  4459. };
  4460. static int __init perf_event_sysfs_init(void)
  4461. {
  4462. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  4463. &perfclass_attr_group);
  4464. }
  4465. device_initcall(perf_event_sysfs_init);