core.c 174 KB

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