perf_event.c 137 KB

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