core.c 161 KB

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