core.c 183 KB

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