core.c 182 KB

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