core.c 174 KB

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