perf_event.c 143 KB

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