core.c 182 KB

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