core.c 173 KB

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