core.c 163 KB

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