perf_event.c 134 KB

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