core.c 167 KB

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