perf_event.c 169 KB

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