perf_event.c 126 KB

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