perf_event.c 152 KB

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