perf_event.c 145 KB

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