core.c 173 KB

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