perf_event.c 143 KB

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