core.c 158 KB

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