perf_event.c 154 KB

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