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