perf_event.c 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721
  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. /* Reuse ptrace permission checks for now. */
  1823. err = -EACCES;
  1824. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1825. goto errout;
  1826. return task;
  1827. errout:
  1828. put_task_struct(task);
  1829. return ERR_PTR(err);
  1830. }
  1831. static struct perf_event_context *
  1832. find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
  1833. {
  1834. struct perf_event_context *ctx;
  1835. struct perf_cpu_context *cpuctx;
  1836. unsigned long flags;
  1837. int ctxn, err;
  1838. if (!task) {
  1839. /* Must be root to operate on a CPU event: */
  1840. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1841. return ERR_PTR(-EACCES);
  1842. /*
  1843. * We could be clever and allow to attach a event to an
  1844. * offline CPU and activate it when the CPU comes up, but
  1845. * that's for later.
  1846. */
  1847. if (!cpu_online(cpu))
  1848. return ERR_PTR(-ENODEV);
  1849. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  1850. ctx = &cpuctx->ctx;
  1851. get_ctx(ctx);
  1852. return ctx;
  1853. }
  1854. err = -EINVAL;
  1855. ctxn = pmu->task_ctx_nr;
  1856. if (ctxn < 0)
  1857. goto errout;
  1858. retry:
  1859. ctx = perf_lock_task_context(task, ctxn, &flags);
  1860. if (ctx) {
  1861. unclone_ctx(ctx);
  1862. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1863. }
  1864. if (!ctx) {
  1865. ctx = alloc_perf_context(pmu, task);
  1866. err = -ENOMEM;
  1867. if (!ctx)
  1868. goto errout;
  1869. get_ctx(ctx);
  1870. err = 0;
  1871. mutex_lock(&task->perf_event_mutex);
  1872. /*
  1873. * If it has already passed perf_event_exit_task().
  1874. * we must see PF_EXITING, it takes this mutex too.
  1875. */
  1876. if (task->flags & PF_EXITING)
  1877. err = -ESRCH;
  1878. else if (task->perf_event_ctxp[ctxn])
  1879. err = -EAGAIN;
  1880. else
  1881. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  1882. mutex_unlock(&task->perf_event_mutex);
  1883. if (unlikely(err)) {
  1884. put_task_struct(task);
  1885. kfree(ctx);
  1886. if (err == -EAGAIN)
  1887. goto retry;
  1888. goto errout;
  1889. }
  1890. }
  1891. return ctx;
  1892. errout:
  1893. return ERR_PTR(err);
  1894. }
  1895. static void perf_event_free_filter(struct perf_event *event);
  1896. static void free_event_rcu(struct rcu_head *head)
  1897. {
  1898. struct perf_event *event;
  1899. event = container_of(head, struct perf_event, rcu_head);
  1900. if (event->ns)
  1901. put_pid_ns(event->ns);
  1902. perf_event_free_filter(event);
  1903. kfree(event);
  1904. }
  1905. static void perf_buffer_put(struct perf_buffer *buffer);
  1906. static void free_event(struct perf_event *event)
  1907. {
  1908. irq_work_sync(&event->pending);
  1909. if (!event->parent) {
  1910. if (event->attach_state & PERF_ATTACH_TASK)
  1911. jump_label_dec(&perf_task_events);
  1912. if (event->attr.mmap || event->attr.mmap_data)
  1913. atomic_dec(&nr_mmap_events);
  1914. if (event->attr.comm)
  1915. atomic_dec(&nr_comm_events);
  1916. if (event->attr.task)
  1917. atomic_dec(&nr_task_events);
  1918. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  1919. put_callchain_buffers();
  1920. }
  1921. if (event->buffer) {
  1922. perf_buffer_put(event->buffer);
  1923. event->buffer = NULL;
  1924. }
  1925. if (event->destroy)
  1926. event->destroy(event);
  1927. if (event->ctx)
  1928. put_ctx(event->ctx);
  1929. call_rcu(&event->rcu_head, free_event_rcu);
  1930. }
  1931. int perf_event_release_kernel(struct perf_event *event)
  1932. {
  1933. struct perf_event_context *ctx = event->ctx;
  1934. /*
  1935. * Remove from the PMU, can't get re-enabled since we got
  1936. * here because the last ref went.
  1937. */
  1938. perf_event_disable(event);
  1939. WARN_ON_ONCE(ctx->parent_ctx);
  1940. /*
  1941. * There are two ways this annotation is useful:
  1942. *
  1943. * 1) there is a lock recursion from perf_event_exit_task
  1944. * see the comment there.
  1945. *
  1946. * 2) there is a lock-inversion with mmap_sem through
  1947. * perf_event_read_group(), which takes faults while
  1948. * holding ctx->mutex, however this is called after
  1949. * the last filedesc died, so there is no possibility
  1950. * to trigger the AB-BA case.
  1951. */
  1952. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  1953. raw_spin_lock_irq(&ctx->lock);
  1954. perf_group_detach(event);
  1955. list_del_event(event, ctx);
  1956. raw_spin_unlock_irq(&ctx->lock);
  1957. mutex_unlock(&ctx->mutex);
  1958. free_event(event);
  1959. return 0;
  1960. }
  1961. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  1962. /*
  1963. * Called when the last reference to the file is gone.
  1964. */
  1965. static int perf_release(struct inode *inode, struct file *file)
  1966. {
  1967. struct perf_event *event = file->private_data;
  1968. struct task_struct *owner;
  1969. file->private_data = NULL;
  1970. rcu_read_lock();
  1971. owner = ACCESS_ONCE(event->owner);
  1972. /*
  1973. * Matches the smp_wmb() in perf_event_exit_task(). If we observe
  1974. * !owner it means the list deletion is complete and we can indeed
  1975. * free this event, otherwise we need to serialize on
  1976. * owner->perf_event_mutex.
  1977. */
  1978. smp_read_barrier_depends();
  1979. if (owner) {
  1980. /*
  1981. * Since delayed_put_task_struct() also drops the last
  1982. * task reference we can safely take a new reference
  1983. * while holding the rcu_read_lock().
  1984. */
  1985. get_task_struct(owner);
  1986. }
  1987. rcu_read_unlock();
  1988. if (owner) {
  1989. mutex_lock(&owner->perf_event_mutex);
  1990. /*
  1991. * We have to re-check the event->owner field, if it is cleared
  1992. * we raced with perf_event_exit_task(), acquiring the mutex
  1993. * ensured they're done, and we can proceed with freeing the
  1994. * event.
  1995. */
  1996. if (event->owner)
  1997. list_del_init(&event->owner_entry);
  1998. mutex_unlock(&owner->perf_event_mutex);
  1999. put_task_struct(owner);
  2000. }
  2001. return perf_event_release_kernel(event);
  2002. }
  2003. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  2004. {
  2005. struct perf_event *child;
  2006. u64 total = 0;
  2007. *enabled = 0;
  2008. *running = 0;
  2009. mutex_lock(&event->child_mutex);
  2010. total += perf_event_read(event);
  2011. *enabled += event->total_time_enabled +
  2012. atomic64_read(&event->child_total_time_enabled);
  2013. *running += event->total_time_running +
  2014. atomic64_read(&event->child_total_time_running);
  2015. list_for_each_entry(child, &event->child_list, child_list) {
  2016. total += perf_event_read(child);
  2017. *enabled += child->total_time_enabled;
  2018. *running += child->total_time_running;
  2019. }
  2020. mutex_unlock(&event->child_mutex);
  2021. return total;
  2022. }
  2023. EXPORT_SYMBOL_GPL(perf_event_read_value);
  2024. static int perf_event_read_group(struct perf_event *event,
  2025. u64 read_format, char __user *buf)
  2026. {
  2027. struct perf_event *leader = event->group_leader, *sub;
  2028. int n = 0, size = 0, ret = -EFAULT;
  2029. struct perf_event_context *ctx = leader->ctx;
  2030. u64 values[5];
  2031. u64 count, enabled, running;
  2032. mutex_lock(&ctx->mutex);
  2033. count = perf_event_read_value(leader, &enabled, &running);
  2034. values[n++] = 1 + leader->nr_siblings;
  2035. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2036. values[n++] = enabled;
  2037. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2038. values[n++] = running;
  2039. values[n++] = count;
  2040. if (read_format & PERF_FORMAT_ID)
  2041. values[n++] = primary_event_id(leader);
  2042. size = n * sizeof(u64);
  2043. if (copy_to_user(buf, values, size))
  2044. goto unlock;
  2045. ret = size;
  2046. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2047. n = 0;
  2048. values[n++] = perf_event_read_value(sub, &enabled, &running);
  2049. if (read_format & PERF_FORMAT_ID)
  2050. values[n++] = primary_event_id(sub);
  2051. size = n * sizeof(u64);
  2052. if (copy_to_user(buf + ret, values, size)) {
  2053. ret = -EFAULT;
  2054. goto unlock;
  2055. }
  2056. ret += size;
  2057. }
  2058. unlock:
  2059. mutex_unlock(&ctx->mutex);
  2060. return ret;
  2061. }
  2062. static int perf_event_read_one(struct perf_event *event,
  2063. u64 read_format, char __user *buf)
  2064. {
  2065. u64 enabled, running;
  2066. u64 values[4];
  2067. int n = 0;
  2068. values[n++] = perf_event_read_value(event, &enabled, &running);
  2069. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2070. values[n++] = enabled;
  2071. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2072. values[n++] = running;
  2073. if (read_format & PERF_FORMAT_ID)
  2074. values[n++] = primary_event_id(event);
  2075. if (copy_to_user(buf, values, n * sizeof(u64)))
  2076. return -EFAULT;
  2077. return n * sizeof(u64);
  2078. }
  2079. /*
  2080. * Read the performance event - simple non blocking version for now
  2081. */
  2082. static ssize_t
  2083. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  2084. {
  2085. u64 read_format = event->attr.read_format;
  2086. int ret;
  2087. /*
  2088. * Return end-of-file for a read on a event that is in
  2089. * error state (i.e. because it was pinned but it couldn't be
  2090. * scheduled on to the CPU at some point).
  2091. */
  2092. if (event->state == PERF_EVENT_STATE_ERROR)
  2093. return 0;
  2094. if (count < event->read_size)
  2095. return -ENOSPC;
  2096. WARN_ON_ONCE(event->ctx->parent_ctx);
  2097. if (read_format & PERF_FORMAT_GROUP)
  2098. ret = perf_event_read_group(event, read_format, buf);
  2099. else
  2100. ret = perf_event_read_one(event, read_format, buf);
  2101. return ret;
  2102. }
  2103. static ssize_t
  2104. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  2105. {
  2106. struct perf_event *event = file->private_data;
  2107. return perf_read_hw(event, buf, count);
  2108. }
  2109. static unsigned int perf_poll(struct file *file, poll_table *wait)
  2110. {
  2111. struct perf_event *event = file->private_data;
  2112. struct perf_buffer *buffer;
  2113. unsigned int events = POLL_HUP;
  2114. rcu_read_lock();
  2115. buffer = rcu_dereference(event->buffer);
  2116. if (buffer)
  2117. events = atomic_xchg(&buffer->poll, 0);
  2118. rcu_read_unlock();
  2119. poll_wait(file, &event->waitq, wait);
  2120. return events;
  2121. }
  2122. static void perf_event_reset(struct perf_event *event)
  2123. {
  2124. (void)perf_event_read(event);
  2125. local64_set(&event->count, 0);
  2126. perf_event_update_userpage(event);
  2127. }
  2128. /*
  2129. * Holding the top-level event's child_mutex means that any
  2130. * descendant process that has inherited this event will block
  2131. * in sync_child_event if it goes to exit, thus satisfying the
  2132. * task existence requirements of perf_event_enable/disable.
  2133. */
  2134. static void perf_event_for_each_child(struct perf_event *event,
  2135. void (*func)(struct perf_event *))
  2136. {
  2137. struct perf_event *child;
  2138. WARN_ON_ONCE(event->ctx->parent_ctx);
  2139. mutex_lock(&event->child_mutex);
  2140. func(event);
  2141. list_for_each_entry(child, &event->child_list, child_list)
  2142. func(child);
  2143. mutex_unlock(&event->child_mutex);
  2144. }
  2145. static void perf_event_for_each(struct perf_event *event,
  2146. void (*func)(struct perf_event *))
  2147. {
  2148. struct perf_event_context *ctx = event->ctx;
  2149. struct perf_event *sibling;
  2150. WARN_ON_ONCE(ctx->parent_ctx);
  2151. mutex_lock(&ctx->mutex);
  2152. event = event->group_leader;
  2153. perf_event_for_each_child(event, func);
  2154. func(event);
  2155. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  2156. perf_event_for_each_child(event, func);
  2157. mutex_unlock(&ctx->mutex);
  2158. }
  2159. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  2160. {
  2161. struct perf_event_context *ctx = event->ctx;
  2162. int ret = 0;
  2163. u64 value;
  2164. if (!is_sampling_event(event))
  2165. return -EINVAL;
  2166. if (copy_from_user(&value, arg, sizeof(value)))
  2167. return -EFAULT;
  2168. if (!value)
  2169. return -EINVAL;
  2170. raw_spin_lock_irq(&ctx->lock);
  2171. if (event->attr.freq) {
  2172. if (value > sysctl_perf_event_sample_rate) {
  2173. ret = -EINVAL;
  2174. goto unlock;
  2175. }
  2176. event->attr.sample_freq = value;
  2177. } else {
  2178. event->attr.sample_period = value;
  2179. event->hw.sample_period = value;
  2180. }
  2181. unlock:
  2182. raw_spin_unlock_irq(&ctx->lock);
  2183. return ret;
  2184. }
  2185. static const struct file_operations perf_fops;
  2186. static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  2187. {
  2188. struct file *file;
  2189. file = fget_light(fd, fput_needed);
  2190. if (!file)
  2191. return ERR_PTR(-EBADF);
  2192. if (file->f_op != &perf_fops) {
  2193. fput_light(file, *fput_needed);
  2194. *fput_needed = 0;
  2195. return ERR_PTR(-EBADF);
  2196. }
  2197. return file->private_data;
  2198. }
  2199. static int perf_event_set_output(struct perf_event *event,
  2200. struct perf_event *output_event);
  2201. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  2202. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2203. {
  2204. struct perf_event *event = file->private_data;
  2205. void (*func)(struct perf_event *);
  2206. u32 flags = arg;
  2207. switch (cmd) {
  2208. case PERF_EVENT_IOC_ENABLE:
  2209. func = perf_event_enable;
  2210. break;
  2211. case PERF_EVENT_IOC_DISABLE:
  2212. func = perf_event_disable;
  2213. break;
  2214. case PERF_EVENT_IOC_RESET:
  2215. func = perf_event_reset;
  2216. break;
  2217. case PERF_EVENT_IOC_REFRESH:
  2218. return perf_event_refresh(event, arg);
  2219. case PERF_EVENT_IOC_PERIOD:
  2220. return perf_event_period(event, (u64 __user *)arg);
  2221. case PERF_EVENT_IOC_SET_OUTPUT:
  2222. {
  2223. struct perf_event *output_event = NULL;
  2224. int fput_needed = 0;
  2225. int ret;
  2226. if (arg != -1) {
  2227. output_event = perf_fget_light(arg, &fput_needed);
  2228. if (IS_ERR(output_event))
  2229. return PTR_ERR(output_event);
  2230. }
  2231. ret = perf_event_set_output(event, output_event);
  2232. if (output_event)
  2233. fput_light(output_event->filp, fput_needed);
  2234. return ret;
  2235. }
  2236. case PERF_EVENT_IOC_SET_FILTER:
  2237. return perf_event_set_filter(event, (void __user *)arg);
  2238. default:
  2239. return -ENOTTY;
  2240. }
  2241. if (flags & PERF_IOC_FLAG_GROUP)
  2242. perf_event_for_each(event, func);
  2243. else
  2244. perf_event_for_each_child(event, func);
  2245. return 0;
  2246. }
  2247. int perf_event_task_enable(void)
  2248. {
  2249. struct perf_event *event;
  2250. mutex_lock(&current->perf_event_mutex);
  2251. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2252. perf_event_for_each_child(event, perf_event_enable);
  2253. mutex_unlock(&current->perf_event_mutex);
  2254. return 0;
  2255. }
  2256. int perf_event_task_disable(void)
  2257. {
  2258. struct perf_event *event;
  2259. mutex_lock(&current->perf_event_mutex);
  2260. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2261. perf_event_for_each_child(event, perf_event_disable);
  2262. mutex_unlock(&current->perf_event_mutex);
  2263. return 0;
  2264. }
  2265. #ifndef PERF_EVENT_INDEX_OFFSET
  2266. # define PERF_EVENT_INDEX_OFFSET 0
  2267. #endif
  2268. static int perf_event_index(struct perf_event *event)
  2269. {
  2270. if (event->hw.state & PERF_HES_STOPPED)
  2271. return 0;
  2272. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2273. return 0;
  2274. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  2275. }
  2276. /*
  2277. * Callers need to ensure there can be no nesting of this function, otherwise
  2278. * the seqlock logic goes bad. We can not serialize this because the arch
  2279. * code calls this from NMI context.
  2280. */
  2281. void perf_event_update_userpage(struct perf_event *event)
  2282. {
  2283. struct perf_event_mmap_page *userpg;
  2284. struct perf_buffer *buffer;
  2285. rcu_read_lock();
  2286. buffer = rcu_dereference(event->buffer);
  2287. if (!buffer)
  2288. goto unlock;
  2289. userpg = buffer->user_page;
  2290. /*
  2291. * Disable preemption so as to not let the corresponding user-space
  2292. * spin too long if we get preempted.
  2293. */
  2294. preempt_disable();
  2295. ++userpg->lock;
  2296. barrier();
  2297. userpg->index = perf_event_index(event);
  2298. userpg->offset = perf_event_count(event);
  2299. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2300. userpg->offset -= local64_read(&event->hw.prev_count);
  2301. userpg->time_enabled = event->total_time_enabled +
  2302. atomic64_read(&event->child_total_time_enabled);
  2303. userpg->time_running = event->total_time_running +
  2304. atomic64_read(&event->child_total_time_running);
  2305. barrier();
  2306. ++userpg->lock;
  2307. preempt_enable();
  2308. unlock:
  2309. rcu_read_unlock();
  2310. }
  2311. static unsigned long perf_data_size(struct perf_buffer *buffer);
  2312. static void
  2313. perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
  2314. {
  2315. long max_size = perf_data_size(buffer);
  2316. if (watermark)
  2317. buffer->watermark = min(max_size, watermark);
  2318. if (!buffer->watermark)
  2319. buffer->watermark = max_size / 2;
  2320. if (flags & PERF_BUFFER_WRITABLE)
  2321. buffer->writable = 1;
  2322. atomic_set(&buffer->refcount, 1);
  2323. }
  2324. #ifndef CONFIG_PERF_USE_VMALLOC
  2325. /*
  2326. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  2327. */
  2328. static struct page *
  2329. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2330. {
  2331. if (pgoff > buffer->nr_pages)
  2332. return NULL;
  2333. if (pgoff == 0)
  2334. return virt_to_page(buffer->user_page);
  2335. return virt_to_page(buffer->data_pages[pgoff - 1]);
  2336. }
  2337. static void *perf_mmap_alloc_page(int cpu)
  2338. {
  2339. struct page *page;
  2340. int node;
  2341. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  2342. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2343. if (!page)
  2344. return NULL;
  2345. return page_address(page);
  2346. }
  2347. static struct perf_buffer *
  2348. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2349. {
  2350. struct perf_buffer *buffer;
  2351. unsigned long size;
  2352. int i;
  2353. size = sizeof(struct perf_buffer);
  2354. size += nr_pages * sizeof(void *);
  2355. buffer = kzalloc(size, GFP_KERNEL);
  2356. if (!buffer)
  2357. goto fail;
  2358. buffer->user_page = perf_mmap_alloc_page(cpu);
  2359. if (!buffer->user_page)
  2360. goto fail_user_page;
  2361. for (i = 0; i < nr_pages; i++) {
  2362. buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
  2363. if (!buffer->data_pages[i])
  2364. goto fail_data_pages;
  2365. }
  2366. buffer->nr_pages = nr_pages;
  2367. perf_buffer_init(buffer, watermark, flags);
  2368. return buffer;
  2369. fail_data_pages:
  2370. for (i--; i >= 0; i--)
  2371. free_page((unsigned long)buffer->data_pages[i]);
  2372. free_page((unsigned long)buffer->user_page);
  2373. fail_user_page:
  2374. kfree(buffer);
  2375. fail:
  2376. return NULL;
  2377. }
  2378. static void perf_mmap_free_page(unsigned long addr)
  2379. {
  2380. struct page *page = virt_to_page((void *)addr);
  2381. page->mapping = NULL;
  2382. __free_page(page);
  2383. }
  2384. static void perf_buffer_free(struct perf_buffer *buffer)
  2385. {
  2386. int i;
  2387. perf_mmap_free_page((unsigned long)buffer->user_page);
  2388. for (i = 0; i < buffer->nr_pages; i++)
  2389. perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
  2390. kfree(buffer);
  2391. }
  2392. static inline int page_order(struct perf_buffer *buffer)
  2393. {
  2394. return 0;
  2395. }
  2396. #else
  2397. /*
  2398. * Back perf_mmap() with vmalloc memory.
  2399. *
  2400. * Required for architectures that have d-cache aliasing issues.
  2401. */
  2402. static inline int page_order(struct perf_buffer *buffer)
  2403. {
  2404. return buffer->page_order;
  2405. }
  2406. static struct page *
  2407. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2408. {
  2409. if (pgoff > (1UL << page_order(buffer)))
  2410. return NULL;
  2411. return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
  2412. }
  2413. static void perf_mmap_unmark_page(void *addr)
  2414. {
  2415. struct page *page = vmalloc_to_page(addr);
  2416. page->mapping = NULL;
  2417. }
  2418. static void perf_buffer_free_work(struct work_struct *work)
  2419. {
  2420. struct perf_buffer *buffer;
  2421. void *base;
  2422. int i, nr;
  2423. buffer = container_of(work, struct perf_buffer, work);
  2424. nr = 1 << page_order(buffer);
  2425. base = buffer->user_page;
  2426. for (i = 0; i < nr + 1; i++)
  2427. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  2428. vfree(base);
  2429. kfree(buffer);
  2430. }
  2431. static void perf_buffer_free(struct perf_buffer *buffer)
  2432. {
  2433. schedule_work(&buffer->work);
  2434. }
  2435. static struct perf_buffer *
  2436. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2437. {
  2438. struct perf_buffer *buffer;
  2439. unsigned long size;
  2440. void *all_buf;
  2441. size = sizeof(struct perf_buffer);
  2442. size += sizeof(void *);
  2443. buffer = kzalloc(size, GFP_KERNEL);
  2444. if (!buffer)
  2445. goto fail;
  2446. INIT_WORK(&buffer->work, perf_buffer_free_work);
  2447. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  2448. if (!all_buf)
  2449. goto fail_all_buf;
  2450. buffer->user_page = all_buf;
  2451. buffer->data_pages[0] = all_buf + PAGE_SIZE;
  2452. buffer->page_order = ilog2(nr_pages);
  2453. buffer->nr_pages = 1;
  2454. perf_buffer_init(buffer, watermark, flags);
  2455. return buffer;
  2456. fail_all_buf:
  2457. kfree(buffer);
  2458. fail:
  2459. return NULL;
  2460. }
  2461. #endif
  2462. static unsigned long perf_data_size(struct perf_buffer *buffer)
  2463. {
  2464. return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
  2465. }
  2466. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2467. {
  2468. struct perf_event *event = vma->vm_file->private_data;
  2469. struct perf_buffer *buffer;
  2470. int ret = VM_FAULT_SIGBUS;
  2471. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2472. if (vmf->pgoff == 0)
  2473. ret = 0;
  2474. return ret;
  2475. }
  2476. rcu_read_lock();
  2477. buffer = rcu_dereference(event->buffer);
  2478. if (!buffer)
  2479. goto unlock;
  2480. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2481. goto unlock;
  2482. vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
  2483. if (!vmf->page)
  2484. goto unlock;
  2485. get_page(vmf->page);
  2486. vmf->page->mapping = vma->vm_file->f_mapping;
  2487. vmf->page->index = vmf->pgoff;
  2488. ret = 0;
  2489. unlock:
  2490. rcu_read_unlock();
  2491. return ret;
  2492. }
  2493. static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
  2494. {
  2495. struct perf_buffer *buffer;
  2496. buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
  2497. perf_buffer_free(buffer);
  2498. }
  2499. static struct perf_buffer *perf_buffer_get(struct perf_event *event)
  2500. {
  2501. struct perf_buffer *buffer;
  2502. rcu_read_lock();
  2503. buffer = rcu_dereference(event->buffer);
  2504. if (buffer) {
  2505. if (!atomic_inc_not_zero(&buffer->refcount))
  2506. buffer = NULL;
  2507. }
  2508. rcu_read_unlock();
  2509. return buffer;
  2510. }
  2511. static void perf_buffer_put(struct perf_buffer *buffer)
  2512. {
  2513. if (!atomic_dec_and_test(&buffer->refcount))
  2514. return;
  2515. call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
  2516. }
  2517. static void perf_mmap_open(struct vm_area_struct *vma)
  2518. {
  2519. struct perf_event *event = vma->vm_file->private_data;
  2520. atomic_inc(&event->mmap_count);
  2521. }
  2522. static void perf_mmap_close(struct vm_area_struct *vma)
  2523. {
  2524. struct perf_event *event = vma->vm_file->private_data;
  2525. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2526. unsigned long size = perf_data_size(event->buffer);
  2527. struct user_struct *user = event->mmap_user;
  2528. struct perf_buffer *buffer = event->buffer;
  2529. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2530. vma->vm_mm->locked_vm -= event->mmap_locked;
  2531. rcu_assign_pointer(event->buffer, NULL);
  2532. mutex_unlock(&event->mmap_mutex);
  2533. perf_buffer_put(buffer);
  2534. free_uid(user);
  2535. }
  2536. }
  2537. static const struct vm_operations_struct perf_mmap_vmops = {
  2538. .open = perf_mmap_open,
  2539. .close = perf_mmap_close,
  2540. .fault = perf_mmap_fault,
  2541. .page_mkwrite = perf_mmap_fault,
  2542. };
  2543. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2544. {
  2545. struct perf_event *event = file->private_data;
  2546. unsigned long user_locked, user_lock_limit;
  2547. struct user_struct *user = current_user();
  2548. unsigned long locked, lock_limit;
  2549. struct perf_buffer *buffer;
  2550. unsigned long vma_size;
  2551. unsigned long nr_pages;
  2552. long user_extra, extra;
  2553. int ret = 0, flags = 0;
  2554. /*
  2555. * Don't allow mmap() of inherited per-task counters. This would
  2556. * create a performance issue due to all children writing to the
  2557. * same buffer.
  2558. */
  2559. if (event->cpu == -1 && event->attr.inherit)
  2560. return -EINVAL;
  2561. if (!(vma->vm_flags & VM_SHARED))
  2562. return -EINVAL;
  2563. vma_size = vma->vm_end - vma->vm_start;
  2564. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2565. /*
  2566. * If we have buffer pages ensure they're a power-of-two number, so we
  2567. * can do bitmasks instead of modulo.
  2568. */
  2569. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2570. return -EINVAL;
  2571. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2572. return -EINVAL;
  2573. if (vma->vm_pgoff != 0)
  2574. return -EINVAL;
  2575. WARN_ON_ONCE(event->ctx->parent_ctx);
  2576. mutex_lock(&event->mmap_mutex);
  2577. if (event->buffer) {
  2578. if (event->buffer->nr_pages == nr_pages)
  2579. atomic_inc(&event->buffer->refcount);
  2580. else
  2581. ret = -EINVAL;
  2582. goto unlock;
  2583. }
  2584. user_extra = nr_pages + 1;
  2585. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2586. /*
  2587. * Increase the limit linearly with more CPUs:
  2588. */
  2589. user_lock_limit *= num_online_cpus();
  2590. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2591. extra = 0;
  2592. if (user_locked > user_lock_limit)
  2593. extra = user_locked - user_lock_limit;
  2594. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2595. lock_limit >>= PAGE_SHIFT;
  2596. locked = vma->vm_mm->locked_vm + extra;
  2597. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2598. !capable(CAP_IPC_LOCK)) {
  2599. ret = -EPERM;
  2600. goto unlock;
  2601. }
  2602. WARN_ON(event->buffer);
  2603. if (vma->vm_flags & VM_WRITE)
  2604. flags |= PERF_BUFFER_WRITABLE;
  2605. buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
  2606. event->cpu, flags);
  2607. if (!buffer) {
  2608. ret = -ENOMEM;
  2609. goto unlock;
  2610. }
  2611. rcu_assign_pointer(event->buffer, buffer);
  2612. atomic_long_add(user_extra, &user->locked_vm);
  2613. event->mmap_locked = extra;
  2614. event->mmap_user = get_current_user();
  2615. vma->vm_mm->locked_vm += event->mmap_locked;
  2616. unlock:
  2617. if (!ret)
  2618. atomic_inc(&event->mmap_count);
  2619. mutex_unlock(&event->mmap_mutex);
  2620. vma->vm_flags |= VM_RESERVED;
  2621. vma->vm_ops = &perf_mmap_vmops;
  2622. return ret;
  2623. }
  2624. static int perf_fasync(int fd, struct file *filp, int on)
  2625. {
  2626. struct inode *inode = filp->f_path.dentry->d_inode;
  2627. struct perf_event *event = filp->private_data;
  2628. int retval;
  2629. mutex_lock(&inode->i_mutex);
  2630. retval = fasync_helper(fd, filp, on, &event->fasync);
  2631. mutex_unlock(&inode->i_mutex);
  2632. if (retval < 0)
  2633. return retval;
  2634. return 0;
  2635. }
  2636. static const struct file_operations perf_fops = {
  2637. .llseek = no_llseek,
  2638. .release = perf_release,
  2639. .read = perf_read,
  2640. .poll = perf_poll,
  2641. .unlocked_ioctl = perf_ioctl,
  2642. .compat_ioctl = perf_ioctl,
  2643. .mmap = perf_mmap,
  2644. .fasync = perf_fasync,
  2645. };
  2646. /*
  2647. * Perf event wakeup
  2648. *
  2649. * If there's data, ensure we set the poll() state and publish everything
  2650. * to user-space before waking everybody up.
  2651. */
  2652. void perf_event_wakeup(struct perf_event *event)
  2653. {
  2654. wake_up_all(&event->waitq);
  2655. if (event->pending_kill) {
  2656. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2657. event->pending_kill = 0;
  2658. }
  2659. }
  2660. static void perf_pending_event(struct irq_work *entry)
  2661. {
  2662. struct perf_event *event = container_of(entry,
  2663. struct perf_event, pending);
  2664. if (event->pending_disable) {
  2665. event->pending_disable = 0;
  2666. __perf_event_disable(event);
  2667. }
  2668. if (event->pending_wakeup) {
  2669. event->pending_wakeup = 0;
  2670. perf_event_wakeup(event);
  2671. }
  2672. }
  2673. /*
  2674. * We assume there is only KVM supporting the callbacks.
  2675. * Later on, we might change it to a list if there is
  2676. * another virtualization implementation supporting the callbacks.
  2677. */
  2678. struct perf_guest_info_callbacks *perf_guest_cbs;
  2679. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2680. {
  2681. perf_guest_cbs = cbs;
  2682. return 0;
  2683. }
  2684. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  2685. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2686. {
  2687. perf_guest_cbs = NULL;
  2688. return 0;
  2689. }
  2690. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  2691. /*
  2692. * Output
  2693. */
  2694. static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
  2695. unsigned long offset, unsigned long head)
  2696. {
  2697. unsigned long mask;
  2698. if (!buffer->writable)
  2699. return true;
  2700. mask = perf_data_size(buffer) - 1;
  2701. offset = (offset - tail) & mask;
  2702. head = (head - tail) & mask;
  2703. if ((int)(head - offset) < 0)
  2704. return false;
  2705. return true;
  2706. }
  2707. static void perf_output_wakeup(struct perf_output_handle *handle)
  2708. {
  2709. atomic_set(&handle->buffer->poll, POLL_IN);
  2710. if (handle->nmi) {
  2711. handle->event->pending_wakeup = 1;
  2712. irq_work_queue(&handle->event->pending);
  2713. } else
  2714. perf_event_wakeup(handle->event);
  2715. }
  2716. /*
  2717. * We need to ensure a later event_id doesn't publish a head when a former
  2718. * event isn't done writing. However since we need to deal with NMIs we
  2719. * cannot fully serialize things.
  2720. *
  2721. * We only publish the head (and generate a wakeup) when the outer-most
  2722. * event completes.
  2723. */
  2724. static void perf_output_get_handle(struct perf_output_handle *handle)
  2725. {
  2726. struct perf_buffer *buffer = handle->buffer;
  2727. preempt_disable();
  2728. local_inc(&buffer->nest);
  2729. handle->wakeup = local_read(&buffer->wakeup);
  2730. }
  2731. static void perf_output_put_handle(struct perf_output_handle *handle)
  2732. {
  2733. struct perf_buffer *buffer = handle->buffer;
  2734. unsigned long head;
  2735. again:
  2736. head = local_read(&buffer->head);
  2737. /*
  2738. * IRQ/NMI can happen here, which means we can miss a head update.
  2739. */
  2740. if (!local_dec_and_test(&buffer->nest))
  2741. goto out;
  2742. /*
  2743. * Publish the known good head. Rely on the full barrier implied
  2744. * by atomic_dec_and_test() order the buffer->head read and this
  2745. * write.
  2746. */
  2747. buffer->user_page->data_head = head;
  2748. /*
  2749. * Now check if we missed an update, rely on the (compiler)
  2750. * barrier in atomic_dec_and_test() to re-read buffer->head.
  2751. */
  2752. if (unlikely(head != local_read(&buffer->head))) {
  2753. local_inc(&buffer->nest);
  2754. goto again;
  2755. }
  2756. if (handle->wakeup != local_read(&buffer->wakeup))
  2757. perf_output_wakeup(handle);
  2758. out:
  2759. preempt_enable();
  2760. }
  2761. __always_inline void perf_output_copy(struct perf_output_handle *handle,
  2762. const void *buf, unsigned int len)
  2763. {
  2764. do {
  2765. unsigned long size = min_t(unsigned long, handle->size, len);
  2766. memcpy(handle->addr, buf, size);
  2767. len -= size;
  2768. handle->addr += size;
  2769. buf += size;
  2770. handle->size -= size;
  2771. if (!handle->size) {
  2772. struct perf_buffer *buffer = handle->buffer;
  2773. handle->page++;
  2774. handle->page &= buffer->nr_pages - 1;
  2775. handle->addr = buffer->data_pages[handle->page];
  2776. handle->size = PAGE_SIZE << page_order(buffer);
  2777. }
  2778. } while (len);
  2779. }
  2780. static void __perf_event_header__init_id(struct perf_event_header *header,
  2781. struct perf_sample_data *data,
  2782. struct perf_event *event)
  2783. {
  2784. u64 sample_type = event->attr.sample_type;
  2785. data->type = sample_type;
  2786. header->size += event->id_header_size;
  2787. if (sample_type & PERF_SAMPLE_TID) {
  2788. /* namespace issues */
  2789. data->tid_entry.pid = perf_event_pid(event, current);
  2790. data->tid_entry.tid = perf_event_tid(event, current);
  2791. }
  2792. if (sample_type & PERF_SAMPLE_TIME)
  2793. data->time = perf_clock();
  2794. if (sample_type & PERF_SAMPLE_ID)
  2795. data->id = primary_event_id(event);
  2796. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2797. data->stream_id = event->id;
  2798. if (sample_type & PERF_SAMPLE_CPU) {
  2799. data->cpu_entry.cpu = raw_smp_processor_id();
  2800. data->cpu_entry.reserved = 0;
  2801. }
  2802. }
  2803. static void perf_event_header__init_id(struct perf_event_header *header,
  2804. struct perf_sample_data *data,
  2805. struct perf_event *event)
  2806. {
  2807. if (event->attr.sample_id_all)
  2808. __perf_event_header__init_id(header, data, event);
  2809. }
  2810. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  2811. struct perf_sample_data *data)
  2812. {
  2813. u64 sample_type = data->type;
  2814. if (sample_type & PERF_SAMPLE_TID)
  2815. perf_output_put(handle, data->tid_entry);
  2816. if (sample_type & PERF_SAMPLE_TIME)
  2817. perf_output_put(handle, data->time);
  2818. if (sample_type & PERF_SAMPLE_ID)
  2819. perf_output_put(handle, data->id);
  2820. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2821. perf_output_put(handle, data->stream_id);
  2822. if (sample_type & PERF_SAMPLE_CPU)
  2823. perf_output_put(handle, data->cpu_entry);
  2824. }
  2825. static void perf_event__output_id_sample(struct perf_event *event,
  2826. struct perf_output_handle *handle,
  2827. struct perf_sample_data *sample)
  2828. {
  2829. if (event->attr.sample_id_all)
  2830. __perf_event__output_id_sample(handle, sample);
  2831. }
  2832. int perf_output_begin(struct perf_output_handle *handle,
  2833. struct perf_event *event, unsigned int size,
  2834. int nmi, int sample)
  2835. {
  2836. struct perf_buffer *buffer;
  2837. unsigned long tail, offset, head;
  2838. int have_lost;
  2839. struct perf_sample_data sample_data;
  2840. struct {
  2841. struct perf_event_header header;
  2842. u64 id;
  2843. u64 lost;
  2844. } lost_event;
  2845. rcu_read_lock();
  2846. /*
  2847. * For inherited events we send all the output towards the parent.
  2848. */
  2849. if (event->parent)
  2850. event = event->parent;
  2851. buffer = rcu_dereference(event->buffer);
  2852. if (!buffer)
  2853. goto out;
  2854. handle->buffer = buffer;
  2855. handle->event = event;
  2856. handle->nmi = nmi;
  2857. handle->sample = sample;
  2858. if (!buffer->nr_pages)
  2859. goto out;
  2860. have_lost = local_read(&buffer->lost);
  2861. if (have_lost) {
  2862. lost_event.header.size = sizeof(lost_event);
  2863. perf_event_header__init_id(&lost_event.header, &sample_data,
  2864. event);
  2865. size += lost_event.header.size;
  2866. }
  2867. perf_output_get_handle(handle);
  2868. do {
  2869. /*
  2870. * Userspace could choose to issue a mb() before updating the
  2871. * tail pointer. So that all reads will be completed before the
  2872. * write is issued.
  2873. */
  2874. tail = ACCESS_ONCE(buffer->user_page->data_tail);
  2875. smp_rmb();
  2876. offset = head = local_read(&buffer->head);
  2877. head += size;
  2878. if (unlikely(!perf_output_space(buffer, tail, offset, head)))
  2879. goto fail;
  2880. } while (local_cmpxchg(&buffer->head, offset, head) != offset);
  2881. if (head - local_read(&buffer->wakeup) > buffer->watermark)
  2882. local_add(buffer->watermark, &buffer->wakeup);
  2883. handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
  2884. handle->page &= buffer->nr_pages - 1;
  2885. handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
  2886. handle->addr = buffer->data_pages[handle->page];
  2887. handle->addr += handle->size;
  2888. handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
  2889. if (have_lost) {
  2890. lost_event.header.type = PERF_RECORD_LOST;
  2891. lost_event.header.misc = 0;
  2892. lost_event.id = event->id;
  2893. lost_event.lost = local_xchg(&buffer->lost, 0);
  2894. perf_output_put(handle, lost_event);
  2895. perf_event__output_id_sample(event, handle, &sample_data);
  2896. }
  2897. return 0;
  2898. fail:
  2899. local_inc(&buffer->lost);
  2900. perf_output_put_handle(handle);
  2901. out:
  2902. rcu_read_unlock();
  2903. return -ENOSPC;
  2904. }
  2905. void perf_output_end(struct perf_output_handle *handle)
  2906. {
  2907. struct perf_event *event = handle->event;
  2908. struct perf_buffer *buffer = handle->buffer;
  2909. int wakeup_events = event->attr.wakeup_events;
  2910. if (handle->sample && wakeup_events) {
  2911. int events = local_inc_return(&buffer->events);
  2912. if (events >= wakeup_events) {
  2913. local_sub(wakeup_events, &buffer->events);
  2914. local_inc(&buffer->wakeup);
  2915. }
  2916. }
  2917. perf_output_put_handle(handle);
  2918. rcu_read_unlock();
  2919. }
  2920. static void perf_output_read_one(struct perf_output_handle *handle,
  2921. struct perf_event *event,
  2922. u64 enabled, u64 running)
  2923. {
  2924. u64 read_format = event->attr.read_format;
  2925. u64 values[4];
  2926. int n = 0;
  2927. values[n++] = perf_event_count(event);
  2928. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2929. values[n++] = enabled +
  2930. atomic64_read(&event->child_total_time_enabled);
  2931. }
  2932. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2933. values[n++] = running +
  2934. atomic64_read(&event->child_total_time_running);
  2935. }
  2936. if (read_format & PERF_FORMAT_ID)
  2937. values[n++] = primary_event_id(event);
  2938. perf_output_copy(handle, values, n * sizeof(u64));
  2939. }
  2940. /*
  2941. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2942. */
  2943. static void perf_output_read_group(struct perf_output_handle *handle,
  2944. struct perf_event *event,
  2945. u64 enabled, u64 running)
  2946. {
  2947. struct perf_event *leader = event->group_leader, *sub;
  2948. u64 read_format = event->attr.read_format;
  2949. u64 values[5];
  2950. int n = 0;
  2951. values[n++] = 1 + leader->nr_siblings;
  2952. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2953. values[n++] = enabled;
  2954. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2955. values[n++] = running;
  2956. if (leader != event)
  2957. leader->pmu->read(leader);
  2958. values[n++] = perf_event_count(leader);
  2959. if (read_format & PERF_FORMAT_ID)
  2960. values[n++] = primary_event_id(leader);
  2961. perf_output_copy(handle, values, n * sizeof(u64));
  2962. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2963. n = 0;
  2964. if (sub != event)
  2965. sub->pmu->read(sub);
  2966. values[n++] = perf_event_count(sub);
  2967. if (read_format & PERF_FORMAT_ID)
  2968. values[n++] = primary_event_id(sub);
  2969. perf_output_copy(handle, values, n * sizeof(u64));
  2970. }
  2971. }
  2972. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  2973. PERF_FORMAT_TOTAL_TIME_RUNNING)
  2974. static void perf_output_read(struct perf_output_handle *handle,
  2975. struct perf_event *event)
  2976. {
  2977. u64 enabled = 0, running = 0, now, ctx_time;
  2978. u64 read_format = event->attr.read_format;
  2979. /*
  2980. * compute total_time_enabled, total_time_running
  2981. * based on snapshot values taken when the event
  2982. * was last scheduled in.
  2983. *
  2984. * we cannot simply called update_context_time()
  2985. * because of locking issue as we are called in
  2986. * NMI context
  2987. */
  2988. if (read_format & PERF_FORMAT_TOTAL_TIMES) {
  2989. now = perf_clock();
  2990. ctx_time = event->shadow_ctx_time + now;
  2991. enabled = ctx_time - event->tstamp_enabled;
  2992. running = ctx_time - event->tstamp_running;
  2993. }
  2994. if (event->attr.read_format & PERF_FORMAT_GROUP)
  2995. perf_output_read_group(handle, event, enabled, running);
  2996. else
  2997. perf_output_read_one(handle, event, enabled, running);
  2998. }
  2999. void perf_output_sample(struct perf_output_handle *handle,
  3000. struct perf_event_header *header,
  3001. struct perf_sample_data *data,
  3002. struct perf_event *event)
  3003. {
  3004. u64 sample_type = data->type;
  3005. perf_output_put(handle, *header);
  3006. if (sample_type & PERF_SAMPLE_IP)
  3007. perf_output_put(handle, data->ip);
  3008. if (sample_type & PERF_SAMPLE_TID)
  3009. perf_output_put(handle, data->tid_entry);
  3010. if (sample_type & PERF_SAMPLE_TIME)
  3011. perf_output_put(handle, data->time);
  3012. if (sample_type & PERF_SAMPLE_ADDR)
  3013. perf_output_put(handle, data->addr);
  3014. if (sample_type & PERF_SAMPLE_ID)
  3015. perf_output_put(handle, data->id);
  3016. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3017. perf_output_put(handle, data->stream_id);
  3018. if (sample_type & PERF_SAMPLE_CPU)
  3019. perf_output_put(handle, data->cpu_entry);
  3020. if (sample_type & PERF_SAMPLE_PERIOD)
  3021. perf_output_put(handle, data->period);
  3022. if (sample_type & PERF_SAMPLE_READ)
  3023. perf_output_read(handle, event);
  3024. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3025. if (data->callchain) {
  3026. int size = 1;
  3027. if (data->callchain)
  3028. size += data->callchain->nr;
  3029. size *= sizeof(u64);
  3030. perf_output_copy(handle, data->callchain, size);
  3031. } else {
  3032. u64 nr = 0;
  3033. perf_output_put(handle, nr);
  3034. }
  3035. }
  3036. if (sample_type & PERF_SAMPLE_RAW) {
  3037. if (data->raw) {
  3038. perf_output_put(handle, data->raw->size);
  3039. perf_output_copy(handle, data->raw->data,
  3040. data->raw->size);
  3041. } else {
  3042. struct {
  3043. u32 size;
  3044. u32 data;
  3045. } raw = {
  3046. .size = sizeof(u32),
  3047. .data = 0,
  3048. };
  3049. perf_output_put(handle, raw);
  3050. }
  3051. }
  3052. }
  3053. void perf_prepare_sample(struct perf_event_header *header,
  3054. struct perf_sample_data *data,
  3055. struct perf_event *event,
  3056. struct pt_regs *regs)
  3057. {
  3058. u64 sample_type = event->attr.sample_type;
  3059. header->type = PERF_RECORD_SAMPLE;
  3060. header->size = sizeof(*header) + event->header_size;
  3061. header->misc = 0;
  3062. header->misc |= perf_misc_flags(regs);
  3063. __perf_event_header__init_id(header, data, event);
  3064. if (sample_type & PERF_SAMPLE_IP)
  3065. data->ip = perf_instruction_pointer(regs);
  3066. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3067. int size = 1;
  3068. data->callchain = perf_callchain(regs);
  3069. if (data->callchain)
  3070. size += data->callchain->nr;
  3071. header->size += size * sizeof(u64);
  3072. }
  3073. if (sample_type & PERF_SAMPLE_RAW) {
  3074. int size = sizeof(u32);
  3075. if (data->raw)
  3076. size += data->raw->size;
  3077. else
  3078. size += sizeof(u32);
  3079. WARN_ON_ONCE(size & (sizeof(u64)-1));
  3080. header->size += size;
  3081. }
  3082. }
  3083. static void perf_event_output(struct perf_event *event, int nmi,
  3084. struct perf_sample_data *data,
  3085. struct pt_regs *regs)
  3086. {
  3087. struct perf_output_handle handle;
  3088. struct perf_event_header header;
  3089. /* protect the callchain buffers */
  3090. rcu_read_lock();
  3091. perf_prepare_sample(&header, data, event, regs);
  3092. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  3093. goto exit;
  3094. perf_output_sample(&handle, &header, data, event);
  3095. perf_output_end(&handle);
  3096. exit:
  3097. rcu_read_unlock();
  3098. }
  3099. /*
  3100. * read event_id
  3101. */
  3102. struct perf_read_event {
  3103. struct perf_event_header header;
  3104. u32 pid;
  3105. u32 tid;
  3106. };
  3107. static void
  3108. perf_event_read_event(struct perf_event *event,
  3109. struct task_struct *task)
  3110. {
  3111. struct perf_output_handle handle;
  3112. struct perf_sample_data sample;
  3113. struct perf_read_event read_event = {
  3114. .header = {
  3115. .type = PERF_RECORD_READ,
  3116. .misc = 0,
  3117. .size = sizeof(read_event) + event->read_size,
  3118. },
  3119. .pid = perf_event_pid(event, task),
  3120. .tid = perf_event_tid(event, task),
  3121. };
  3122. int ret;
  3123. perf_event_header__init_id(&read_event.header, &sample, event);
  3124. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  3125. if (ret)
  3126. return;
  3127. perf_output_put(&handle, read_event);
  3128. perf_output_read(&handle, event);
  3129. perf_event__output_id_sample(event, &handle, &sample);
  3130. perf_output_end(&handle);
  3131. }
  3132. /*
  3133. * task tracking -- fork/exit
  3134. *
  3135. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  3136. */
  3137. struct perf_task_event {
  3138. struct task_struct *task;
  3139. struct perf_event_context *task_ctx;
  3140. struct {
  3141. struct perf_event_header header;
  3142. u32 pid;
  3143. u32 ppid;
  3144. u32 tid;
  3145. u32 ptid;
  3146. u64 time;
  3147. } event_id;
  3148. };
  3149. static void perf_event_task_output(struct perf_event *event,
  3150. struct perf_task_event *task_event)
  3151. {
  3152. struct perf_output_handle handle;
  3153. struct perf_sample_data sample;
  3154. struct task_struct *task = task_event->task;
  3155. int ret, size = task_event->event_id.header.size;
  3156. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  3157. ret = perf_output_begin(&handle, event,
  3158. task_event->event_id.header.size, 0, 0);
  3159. if (ret)
  3160. goto out;
  3161. task_event->event_id.pid = perf_event_pid(event, task);
  3162. task_event->event_id.ppid = perf_event_pid(event, current);
  3163. task_event->event_id.tid = perf_event_tid(event, task);
  3164. task_event->event_id.ptid = perf_event_tid(event, current);
  3165. perf_output_put(&handle, task_event->event_id);
  3166. perf_event__output_id_sample(event, &handle, &sample);
  3167. perf_output_end(&handle);
  3168. out:
  3169. task_event->event_id.header.size = size;
  3170. }
  3171. static int perf_event_task_match(struct perf_event *event)
  3172. {
  3173. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3174. return 0;
  3175. if (!event_filter_match(event))
  3176. return 0;
  3177. if (event->attr.comm || event->attr.mmap ||
  3178. event->attr.mmap_data || event->attr.task)
  3179. return 1;
  3180. return 0;
  3181. }
  3182. static void perf_event_task_ctx(struct perf_event_context *ctx,
  3183. struct perf_task_event *task_event)
  3184. {
  3185. struct perf_event *event;
  3186. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3187. if (perf_event_task_match(event))
  3188. perf_event_task_output(event, task_event);
  3189. }
  3190. }
  3191. static void perf_event_task_event(struct perf_task_event *task_event)
  3192. {
  3193. struct perf_cpu_context *cpuctx;
  3194. struct perf_event_context *ctx;
  3195. struct pmu *pmu;
  3196. int ctxn;
  3197. rcu_read_lock();
  3198. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3199. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3200. if (cpuctx->active_pmu != pmu)
  3201. goto next;
  3202. perf_event_task_ctx(&cpuctx->ctx, task_event);
  3203. ctx = task_event->task_ctx;
  3204. if (!ctx) {
  3205. ctxn = pmu->task_ctx_nr;
  3206. if (ctxn < 0)
  3207. goto next;
  3208. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3209. }
  3210. if (ctx)
  3211. perf_event_task_ctx(ctx, task_event);
  3212. next:
  3213. put_cpu_ptr(pmu->pmu_cpu_context);
  3214. }
  3215. rcu_read_unlock();
  3216. }
  3217. static void perf_event_task(struct task_struct *task,
  3218. struct perf_event_context *task_ctx,
  3219. int new)
  3220. {
  3221. struct perf_task_event task_event;
  3222. if (!atomic_read(&nr_comm_events) &&
  3223. !atomic_read(&nr_mmap_events) &&
  3224. !atomic_read(&nr_task_events))
  3225. return;
  3226. task_event = (struct perf_task_event){
  3227. .task = task,
  3228. .task_ctx = task_ctx,
  3229. .event_id = {
  3230. .header = {
  3231. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  3232. .misc = 0,
  3233. .size = sizeof(task_event.event_id),
  3234. },
  3235. /* .pid */
  3236. /* .ppid */
  3237. /* .tid */
  3238. /* .ptid */
  3239. .time = perf_clock(),
  3240. },
  3241. };
  3242. perf_event_task_event(&task_event);
  3243. }
  3244. void perf_event_fork(struct task_struct *task)
  3245. {
  3246. perf_event_task(task, NULL, 1);
  3247. }
  3248. /*
  3249. * comm tracking
  3250. */
  3251. struct perf_comm_event {
  3252. struct task_struct *task;
  3253. char *comm;
  3254. int comm_size;
  3255. struct {
  3256. struct perf_event_header header;
  3257. u32 pid;
  3258. u32 tid;
  3259. } event_id;
  3260. };
  3261. static void perf_event_comm_output(struct perf_event *event,
  3262. struct perf_comm_event *comm_event)
  3263. {
  3264. struct perf_output_handle handle;
  3265. struct perf_sample_data sample;
  3266. int size = comm_event->event_id.header.size;
  3267. int ret;
  3268. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  3269. ret = perf_output_begin(&handle, event,
  3270. comm_event->event_id.header.size, 0, 0);
  3271. if (ret)
  3272. goto out;
  3273. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  3274. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  3275. perf_output_put(&handle, comm_event->event_id);
  3276. perf_output_copy(&handle, comm_event->comm,
  3277. comm_event->comm_size);
  3278. perf_event__output_id_sample(event, &handle, &sample);
  3279. perf_output_end(&handle);
  3280. out:
  3281. comm_event->event_id.header.size = size;
  3282. }
  3283. static int perf_event_comm_match(struct perf_event *event)
  3284. {
  3285. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3286. return 0;
  3287. if (!event_filter_match(event))
  3288. return 0;
  3289. if (event->attr.comm)
  3290. return 1;
  3291. return 0;
  3292. }
  3293. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  3294. struct perf_comm_event *comm_event)
  3295. {
  3296. struct perf_event *event;
  3297. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3298. if (perf_event_comm_match(event))
  3299. perf_event_comm_output(event, comm_event);
  3300. }
  3301. }
  3302. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  3303. {
  3304. struct perf_cpu_context *cpuctx;
  3305. struct perf_event_context *ctx;
  3306. char comm[TASK_COMM_LEN];
  3307. unsigned int size;
  3308. struct pmu *pmu;
  3309. int ctxn;
  3310. memset(comm, 0, sizeof(comm));
  3311. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  3312. size = ALIGN(strlen(comm)+1, sizeof(u64));
  3313. comm_event->comm = comm;
  3314. comm_event->comm_size = size;
  3315. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  3316. rcu_read_lock();
  3317. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3318. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3319. if (cpuctx->active_pmu != pmu)
  3320. goto next;
  3321. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  3322. ctxn = pmu->task_ctx_nr;
  3323. if (ctxn < 0)
  3324. goto next;
  3325. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3326. if (ctx)
  3327. perf_event_comm_ctx(ctx, comm_event);
  3328. next:
  3329. put_cpu_ptr(pmu->pmu_cpu_context);
  3330. }
  3331. rcu_read_unlock();
  3332. }
  3333. void perf_event_comm(struct task_struct *task)
  3334. {
  3335. struct perf_comm_event comm_event;
  3336. struct perf_event_context *ctx;
  3337. int ctxn;
  3338. for_each_task_context_nr(ctxn) {
  3339. ctx = task->perf_event_ctxp[ctxn];
  3340. if (!ctx)
  3341. continue;
  3342. perf_event_enable_on_exec(ctx);
  3343. }
  3344. if (!atomic_read(&nr_comm_events))
  3345. return;
  3346. comm_event = (struct perf_comm_event){
  3347. .task = task,
  3348. /* .comm */
  3349. /* .comm_size */
  3350. .event_id = {
  3351. .header = {
  3352. .type = PERF_RECORD_COMM,
  3353. .misc = 0,
  3354. /* .size */
  3355. },
  3356. /* .pid */
  3357. /* .tid */
  3358. },
  3359. };
  3360. perf_event_comm_event(&comm_event);
  3361. }
  3362. /*
  3363. * mmap tracking
  3364. */
  3365. struct perf_mmap_event {
  3366. struct vm_area_struct *vma;
  3367. const char *file_name;
  3368. int file_size;
  3369. struct {
  3370. struct perf_event_header header;
  3371. u32 pid;
  3372. u32 tid;
  3373. u64 start;
  3374. u64 len;
  3375. u64 pgoff;
  3376. } event_id;
  3377. };
  3378. static void perf_event_mmap_output(struct perf_event *event,
  3379. struct perf_mmap_event *mmap_event)
  3380. {
  3381. struct perf_output_handle handle;
  3382. struct perf_sample_data sample;
  3383. int size = mmap_event->event_id.header.size;
  3384. int ret;
  3385. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  3386. ret = perf_output_begin(&handle, event,
  3387. mmap_event->event_id.header.size, 0, 0);
  3388. if (ret)
  3389. goto out;
  3390. mmap_event->event_id.pid = perf_event_pid(event, current);
  3391. mmap_event->event_id.tid = perf_event_tid(event, current);
  3392. perf_output_put(&handle, mmap_event->event_id);
  3393. perf_output_copy(&handle, mmap_event->file_name,
  3394. mmap_event->file_size);
  3395. perf_event__output_id_sample(event, &handle, &sample);
  3396. perf_output_end(&handle);
  3397. out:
  3398. mmap_event->event_id.header.size = size;
  3399. }
  3400. static int perf_event_mmap_match(struct perf_event *event,
  3401. struct perf_mmap_event *mmap_event,
  3402. int executable)
  3403. {
  3404. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3405. return 0;
  3406. if (!event_filter_match(event))
  3407. return 0;
  3408. if ((!executable && event->attr.mmap_data) ||
  3409. (executable && event->attr.mmap))
  3410. return 1;
  3411. return 0;
  3412. }
  3413. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3414. struct perf_mmap_event *mmap_event,
  3415. int executable)
  3416. {
  3417. struct perf_event *event;
  3418. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3419. if (perf_event_mmap_match(event, mmap_event, executable))
  3420. perf_event_mmap_output(event, mmap_event);
  3421. }
  3422. }
  3423. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3424. {
  3425. struct perf_cpu_context *cpuctx;
  3426. struct perf_event_context *ctx;
  3427. struct vm_area_struct *vma = mmap_event->vma;
  3428. struct file *file = vma->vm_file;
  3429. unsigned int size;
  3430. char tmp[16];
  3431. char *buf = NULL;
  3432. const char *name;
  3433. struct pmu *pmu;
  3434. int ctxn;
  3435. memset(tmp, 0, sizeof(tmp));
  3436. if (file) {
  3437. /*
  3438. * d_path works from the end of the buffer backwards, so we
  3439. * need to add enough zero bytes after the string to handle
  3440. * the 64bit alignment we do later.
  3441. */
  3442. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3443. if (!buf) {
  3444. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3445. goto got_name;
  3446. }
  3447. name = d_path(&file->f_path, buf, PATH_MAX);
  3448. if (IS_ERR(name)) {
  3449. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3450. goto got_name;
  3451. }
  3452. } else {
  3453. if (arch_vma_name(mmap_event->vma)) {
  3454. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3455. sizeof(tmp));
  3456. goto got_name;
  3457. }
  3458. if (!vma->vm_mm) {
  3459. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3460. goto got_name;
  3461. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  3462. vma->vm_end >= vma->vm_mm->brk) {
  3463. name = strncpy(tmp, "[heap]", sizeof(tmp));
  3464. goto got_name;
  3465. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  3466. vma->vm_end >= vma->vm_mm->start_stack) {
  3467. name = strncpy(tmp, "[stack]", sizeof(tmp));
  3468. goto got_name;
  3469. }
  3470. name = strncpy(tmp, "//anon", sizeof(tmp));
  3471. goto got_name;
  3472. }
  3473. got_name:
  3474. size = ALIGN(strlen(name)+1, sizeof(u64));
  3475. mmap_event->file_name = name;
  3476. mmap_event->file_size = size;
  3477. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3478. rcu_read_lock();
  3479. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3480. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3481. if (cpuctx->active_pmu != pmu)
  3482. goto next;
  3483. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
  3484. vma->vm_flags & VM_EXEC);
  3485. ctxn = pmu->task_ctx_nr;
  3486. if (ctxn < 0)
  3487. goto next;
  3488. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3489. if (ctx) {
  3490. perf_event_mmap_ctx(ctx, mmap_event,
  3491. vma->vm_flags & VM_EXEC);
  3492. }
  3493. next:
  3494. put_cpu_ptr(pmu->pmu_cpu_context);
  3495. }
  3496. rcu_read_unlock();
  3497. kfree(buf);
  3498. }
  3499. void perf_event_mmap(struct vm_area_struct *vma)
  3500. {
  3501. struct perf_mmap_event mmap_event;
  3502. if (!atomic_read(&nr_mmap_events))
  3503. return;
  3504. mmap_event = (struct perf_mmap_event){
  3505. .vma = vma,
  3506. /* .file_name */
  3507. /* .file_size */
  3508. .event_id = {
  3509. .header = {
  3510. .type = PERF_RECORD_MMAP,
  3511. .misc = PERF_RECORD_MISC_USER,
  3512. /* .size */
  3513. },
  3514. /* .pid */
  3515. /* .tid */
  3516. .start = vma->vm_start,
  3517. .len = vma->vm_end - vma->vm_start,
  3518. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3519. },
  3520. };
  3521. perf_event_mmap_event(&mmap_event);
  3522. }
  3523. /*
  3524. * IRQ throttle logging
  3525. */
  3526. static void perf_log_throttle(struct perf_event *event, int enable)
  3527. {
  3528. struct perf_output_handle handle;
  3529. struct perf_sample_data sample;
  3530. int ret;
  3531. struct {
  3532. struct perf_event_header header;
  3533. u64 time;
  3534. u64 id;
  3535. u64 stream_id;
  3536. } throttle_event = {
  3537. .header = {
  3538. .type = PERF_RECORD_THROTTLE,
  3539. .misc = 0,
  3540. .size = sizeof(throttle_event),
  3541. },
  3542. .time = perf_clock(),
  3543. .id = primary_event_id(event),
  3544. .stream_id = event->id,
  3545. };
  3546. if (enable)
  3547. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3548. perf_event_header__init_id(&throttle_event.header, &sample, event);
  3549. ret = perf_output_begin(&handle, event,
  3550. throttle_event.header.size, 1, 0);
  3551. if (ret)
  3552. return;
  3553. perf_output_put(&handle, throttle_event);
  3554. perf_event__output_id_sample(event, &handle, &sample);
  3555. perf_output_end(&handle);
  3556. }
  3557. /*
  3558. * Generic event overflow handling, sampling.
  3559. */
  3560. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3561. int throttle, struct perf_sample_data *data,
  3562. struct pt_regs *regs)
  3563. {
  3564. int events = atomic_read(&event->event_limit);
  3565. struct hw_perf_event *hwc = &event->hw;
  3566. int ret = 0;
  3567. /*
  3568. * Non-sampling counters might still use the PMI to fold short
  3569. * hardware counters, ignore those.
  3570. */
  3571. if (unlikely(!is_sampling_event(event)))
  3572. return 0;
  3573. if (!throttle) {
  3574. hwc->interrupts++;
  3575. } else {
  3576. if (hwc->interrupts != MAX_INTERRUPTS) {
  3577. hwc->interrupts++;
  3578. if (HZ * hwc->interrupts >
  3579. (u64)sysctl_perf_event_sample_rate) {
  3580. hwc->interrupts = MAX_INTERRUPTS;
  3581. perf_log_throttle(event, 0);
  3582. ret = 1;
  3583. }
  3584. } else {
  3585. /*
  3586. * Keep re-disabling events even though on the previous
  3587. * pass we disabled it - just in case we raced with a
  3588. * sched-in and the event got enabled again:
  3589. */
  3590. ret = 1;
  3591. }
  3592. }
  3593. if (event->attr.freq) {
  3594. u64 now = perf_clock();
  3595. s64 delta = now - hwc->freq_time_stamp;
  3596. hwc->freq_time_stamp = now;
  3597. if (delta > 0 && delta < 2*TICK_NSEC)
  3598. perf_adjust_period(event, delta, hwc->last_period);
  3599. }
  3600. /*
  3601. * XXX event_limit might not quite work as expected on inherited
  3602. * events
  3603. */
  3604. event->pending_kill = POLL_IN;
  3605. if (events && atomic_dec_and_test(&event->event_limit)) {
  3606. ret = 1;
  3607. event->pending_kill = POLL_HUP;
  3608. if (nmi) {
  3609. event->pending_disable = 1;
  3610. irq_work_queue(&event->pending);
  3611. } else
  3612. perf_event_disable(event);
  3613. }
  3614. if (event->overflow_handler)
  3615. event->overflow_handler(event, nmi, data, regs);
  3616. else
  3617. perf_event_output(event, nmi, data, regs);
  3618. return ret;
  3619. }
  3620. int perf_event_overflow(struct perf_event *event, int nmi,
  3621. struct perf_sample_data *data,
  3622. struct pt_regs *regs)
  3623. {
  3624. return __perf_event_overflow(event, nmi, 1, data, regs);
  3625. }
  3626. /*
  3627. * Generic software event infrastructure
  3628. */
  3629. struct swevent_htable {
  3630. struct swevent_hlist *swevent_hlist;
  3631. struct mutex hlist_mutex;
  3632. int hlist_refcount;
  3633. /* Recursion avoidance in each contexts */
  3634. int recursion[PERF_NR_CONTEXTS];
  3635. };
  3636. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  3637. /*
  3638. * We directly increment event->count and keep a second value in
  3639. * event->hw.period_left to count intervals. This period event
  3640. * is kept in the range [-sample_period, 0] so that we can use the
  3641. * sign as trigger.
  3642. */
  3643. static u64 perf_swevent_set_period(struct perf_event *event)
  3644. {
  3645. struct hw_perf_event *hwc = &event->hw;
  3646. u64 period = hwc->last_period;
  3647. u64 nr, offset;
  3648. s64 old, val;
  3649. hwc->last_period = hwc->sample_period;
  3650. again:
  3651. old = val = local64_read(&hwc->period_left);
  3652. if (val < 0)
  3653. return 0;
  3654. nr = div64_u64(period + val, period);
  3655. offset = nr * period;
  3656. val -= offset;
  3657. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  3658. goto again;
  3659. return nr;
  3660. }
  3661. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3662. int nmi, struct perf_sample_data *data,
  3663. struct pt_regs *regs)
  3664. {
  3665. struct hw_perf_event *hwc = &event->hw;
  3666. int throttle = 0;
  3667. data->period = event->hw.last_period;
  3668. if (!overflow)
  3669. overflow = perf_swevent_set_period(event);
  3670. if (hwc->interrupts == MAX_INTERRUPTS)
  3671. return;
  3672. for (; overflow; overflow--) {
  3673. if (__perf_event_overflow(event, nmi, throttle,
  3674. data, regs)) {
  3675. /*
  3676. * We inhibit the overflow from happening when
  3677. * hwc->interrupts == MAX_INTERRUPTS.
  3678. */
  3679. break;
  3680. }
  3681. throttle = 1;
  3682. }
  3683. }
  3684. static void perf_swevent_event(struct perf_event *event, u64 nr,
  3685. int nmi, struct perf_sample_data *data,
  3686. struct pt_regs *regs)
  3687. {
  3688. struct hw_perf_event *hwc = &event->hw;
  3689. local64_add(nr, &event->count);
  3690. if (!regs)
  3691. return;
  3692. if (!is_sampling_event(event))
  3693. return;
  3694. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3695. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3696. if (local64_add_negative(nr, &hwc->period_left))
  3697. return;
  3698. perf_swevent_overflow(event, 0, nmi, data, regs);
  3699. }
  3700. static int perf_exclude_event(struct perf_event *event,
  3701. struct pt_regs *regs)
  3702. {
  3703. if (event->hw.state & PERF_HES_STOPPED)
  3704. return 0;
  3705. if (regs) {
  3706. if (event->attr.exclude_user && user_mode(regs))
  3707. return 1;
  3708. if (event->attr.exclude_kernel && !user_mode(regs))
  3709. return 1;
  3710. }
  3711. return 0;
  3712. }
  3713. static int perf_swevent_match(struct perf_event *event,
  3714. enum perf_type_id type,
  3715. u32 event_id,
  3716. struct perf_sample_data *data,
  3717. struct pt_regs *regs)
  3718. {
  3719. if (event->attr.type != type)
  3720. return 0;
  3721. if (event->attr.config != event_id)
  3722. return 0;
  3723. if (perf_exclude_event(event, regs))
  3724. return 0;
  3725. return 1;
  3726. }
  3727. static inline u64 swevent_hash(u64 type, u32 event_id)
  3728. {
  3729. u64 val = event_id | (type << 32);
  3730. return hash_64(val, SWEVENT_HLIST_BITS);
  3731. }
  3732. static inline struct hlist_head *
  3733. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3734. {
  3735. u64 hash = swevent_hash(type, event_id);
  3736. return &hlist->heads[hash];
  3737. }
  3738. /* For the read side: events when they trigger */
  3739. static inline struct hlist_head *
  3740. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  3741. {
  3742. struct swevent_hlist *hlist;
  3743. hlist = rcu_dereference(swhash->swevent_hlist);
  3744. if (!hlist)
  3745. return NULL;
  3746. return __find_swevent_head(hlist, type, event_id);
  3747. }
  3748. /* For the event head insertion and removal in the hlist */
  3749. static inline struct hlist_head *
  3750. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  3751. {
  3752. struct swevent_hlist *hlist;
  3753. u32 event_id = event->attr.config;
  3754. u64 type = event->attr.type;
  3755. /*
  3756. * Event scheduling is always serialized against hlist allocation
  3757. * and release. Which makes the protected version suitable here.
  3758. * The context lock guarantees that.
  3759. */
  3760. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  3761. lockdep_is_held(&event->ctx->lock));
  3762. if (!hlist)
  3763. return NULL;
  3764. return __find_swevent_head(hlist, type, event_id);
  3765. }
  3766. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3767. u64 nr, int nmi,
  3768. struct perf_sample_data *data,
  3769. struct pt_regs *regs)
  3770. {
  3771. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3772. struct perf_event *event;
  3773. struct hlist_node *node;
  3774. struct hlist_head *head;
  3775. rcu_read_lock();
  3776. head = find_swevent_head_rcu(swhash, type, event_id);
  3777. if (!head)
  3778. goto end;
  3779. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3780. if (perf_swevent_match(event, type, event_id, data, regs))
  3781. perf_swevent_event(event, nr, nmi, data, regs);
  3782. }
  3783. end:
  3784. rcu_read_unlock();
  3785. }
  3786. int perf_swevent_get_recursion_context(void)
  3787. {
  3788. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3789. return get_recursion_context(swhash->recursion);
  3790. }
  3791. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3792. inline void perf_swevent_put_recursion_context(int rctx)
  3793. {
  3794. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3795. put_recursion_context(swhash->recursion, rctx);
  3796. }
  3797. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3798. struct pt_regs *regs, u64 addr)
  3799. {
  3800. struct perf_sample_data data;
  3801. int rctx;
  3802. preempt_disable_notrace();
  3803. rctx = perf_swevent_get_recursion_context();
  3804. if (rctx < 0)
  3805. return;
  3806. perf_sample_data_init(&data, addr);
  3807. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3808. perf_swevent_put_recursion_context(rctx);
  3809. preempt_enable_notrace();
  3810. }
  3811. static void perf_swevent_read(struct perf_event *event)
  3812. {
  3813. }
  3814. static int perf_swevent_add(struct perf_event *event, int flags)
  3815. {
  3816. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3817. struct hw_perf_event *hwc = &event->hw;
  3818. struct hlist_head *head;
  3819. if (is_sampling_event(event)) {
  3820. hwc->last_period = hwc->sample_period;
  3821. perf_swevent_set_period(event);
  3822. }
  3823. hwc->state = !(flags & PERF_EF_START);
  3824. head = find_swevent_head(swhash, event);
  3825. if (WARN_ON_ONCE(!head))
  3826. return -EINVAL;
  3827. hlist_add_head_rcu(&event->hlist_entry, head);
  3828. return 0;
  3829. }
  3830. static void perf_swevent_del(struct perf_event *event, int flags)
  3831. {
  3832. hlist_del_rcu(&event->hlist_entry);
  3833. }
  3834. static void perf_swevent_start(struct perf_event *event, int flags)
  3835. {
  3836. event->hw.state = 0;
  3837. }
  3838. static void perf_swevent_stop(struct perf_event *event, int flags)
  3839. {
  3840. event->hw.state = PERF_HES_STOPPED;
  3841. }
  3842. /* Deref the hlist from the update side */
  3843. static inline struct swevent_hlist *
  3844. swevent_hlist_deref(struct swevent_htable *swhash)
  3845. {
  3846. return rcu_dereference_protected(swhash->swevent_hlist,
  3847. lockdep_is_held(&swhash->hlist_mutex));
  3848. }
  3849. static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
  3850. {
  3851. struct swevent_hlist *hlist;
  3852. hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
  3853. kfree(hlist);
  3854. }
  3855. static void swevent_hlist_release(struct swevent_htable *swhash)
  3856. {
  3857. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  3858. if (!hlist)
  3859. return;
  3860. rcu_assign_pointer(swhash->swevent_hlist, NULL);
  3861. call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
  3862. }
  3863. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  3864. {
  3865. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3866. mutex_lock(&swhash->hlist_mutex);
  3867. if (!--swhash->hlist_refcount)
  3868. swevent_hlist_release(swhash);
  3869. mutex_unlock(&swhash->hlist_mutex);
  3870. }
  3871. static void swevent_hlist_put(struct perf_event *event)
  3872. {
  3873. int cpu;
  3874. if (event->cpu != -1) {
  3875. swevent_hlist_put_cpu(event, event->cpu);
  3876. return;
  3877. }
  3878. for_each_possible_cpu(cpu)
  3879. swevent_hlist_put_cpu(event, cpu);
  3880. }
  3881. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  3882. {
  3883. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3884. int err = 0;
  3885. mutex_lock(&swhash->hlist_mutex);
  3886. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  3887. struct swevent_hlist *hlist;
  3888. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  3889. if (!hlist) {
  3890. err = -ENOMEM;
  3891. goto exit;
  3892. }
  3893. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  3894. }
  3895. swhash->hlist_refcount++;
  3896. exit:
  3897. mutex_unlock(&swhash->hlist_mutex);
  3898. return err;
  3899. }
  3900. static int swevent_hlist_get(struct perf_event *event)
  3901. {
  3902. int err;
  3903. int cpu, failed_cpu;
  3904. if (event->cpu != -1)
  3905. return swevent_hlist_get_cpu(event, event->cpu);
  3906. get_online_cpus();
  3907. for_each_possible_cpu(cpu) {
  3908. err = swevent_hlist_get_cpu(event, cpu);
  3909. if (err) {
  3910. failed_cpu = cpu;
  3911. goto fail;
  3912. }
  3913. }
  3914. put_online_cpus();
  3915. return 0;
  3916. fail:
  3917. for_each_possible_cpu(cpu) {
  3918. if (cpu == failed_cpu)
  3919. break;
  3920. swevent_hlist_put_cpu(event, cpu);
  3921. }
  3922. put_online_cpus();
  3923. return err;
  3924. }
  3925. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3926. static void sw_perf_event_destroy(struct perf_event *event)
  3927. {
  3928. u64 event_id = event->attr.config;
  3929. WARN_ON(event->parent);
  3930. jump_label_dec(&perf_swevent_enabled[event_id]);
  3931. swevent_hlist_put(event);
  3932. }
  3933. static int perf_swevent_init(struct perf_event *event)
  3934. {
  3935. int event_id = event->attr.config;
  3936. if (event->attr.type != PERF_TYPE_SOFTWARE)
  3937. return -ENOENT;
  3938. switch (event_id) {
  3939. case PERF_COUNT_SW_CPU_CLOCK:
  3940. case PERF_COUNT_SW_TASK_CLOCK:
  3941. return -ENOENT;
  3942. default:
  3943. break;
  3944. }
  3945. if (event_id >= PERF_COUNT_SW_MAX)
  3946. return -ENOENT;
  3947. if (!event->parent) {
  3948. int err;
  3949. err = swevent_hlist_get(event);
  3950. if (err)
  3951. return err;
  3952. jump_label_inc(&perf_swevent_enabled[event_id]);
  3953. event->destroy = sw_perf_event_destroy;
  3954. }
  3955. return 0;
  3956. }
  3957. static struct pmu perf_swevent = {
  3958. .task_ctx_nr = perf_sw_context,
  3959. .event_init = perf_swevent_init,
  3960. .add = perf_swevent_add,
  3961. .del = perf_swevent_del,
  3962. .start = perf_swevent_start,
  3963. .stop = perf_swevent_stop,
  3964. .read = perf_swevent_read,
  3965. };
  3966. #ifdef CONFIG_EVENT_TRACING
  3967. static int perf_tp_filter_match(struct perf_event *event,
  3968. struct perf_sample_data *data)
  3969. {
  3970. void *record = data->raw->data;
  3971. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3972. return 1;
  3973. return 0;
  3974. }
  3975. static int perf_tp_event_match(struct perf_event *event,
  3976. struct perf_sample_data *data,
  3977. struct pt_regs *regs)
  3978. {
  3979. /*
  3980. * All tracepoints are from kernel-space.
  3981. */
  3982. if (event->attr.exclude_kernel)
  3983. return 0;
  3984. if (!perf_tp_filter_match(event, data))
  3985. return 0;
  3986. return 1;
  3987. }
  3988. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  3989. struct pt_regs *regs, struct hlist_head *head, int rctx)
  3990. {
  3991. struct perf_sample_data data;
  3992. struct perf_event *event;
  3993. struct hlist_node *node;
  3994. struct perf_raw_record raw = {
  3995. .size = entry_size,
  3996. .data = record,
  3997. };
  3998. perf_sample_data_init(&data, addr);
  3999. data.raw = &raw;
  4000. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  4001. if (perf_tp_event_match(event, &data, regs))
  4002. perf_swevent_event(event, count, 1, &data, regs);
  4003. }
  4004. perf_swevent_put_recursion_context(rctx);
  4005. }
  4006. EXPORT_SYMBOL_GPL(perf_tp_event);
  4007. static void tp_perf_event_destroy(struct perf_event *event)
  4008. {
  4009. perf_trace_destroy(event);
  4010. }
  4011. static int perf_tp_event_init(struct perf_event *event)
  4012. {
  4013. int err;
  4014. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4015. return -ENOENT;
  4016. err = perf_trace_init(event);
  4017. if (err)
  4018. return err;
  4019. event->destroy = tp_perf_event_destroy;
  4020. return 0;
  4021. }
  4022. static struct pmu perf_tracepoint = {
  4023. .task_ctx_nr = perf_sw_context,
  4024. .event_init = perf_tp_event_init,
  4025. .add = perf_trace_add,
  4026. .del = perf_trace_del,
  4027. .start = perf_swevent_start,
  4028. .stop = perf_swevent_stop,
  4029. .read = perf_swevent_read,
  4030. };
  4031. static inline void perf_tp_register(void)
  4032. {
  4033. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  4034. }
  4035. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4036. {
  4037. char *filter_str;
  4038. int ret;
  4039. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4040. return -EINVAL;
  4041. filter_str = strndup_user(arg, PAGE_SIZE);
  4042. if (IS_ERR(filter_str))
  4043. return PTR_ERR(filter_str);
  4044. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  4045. kfree(filter_str);
  4046. return ret;
  4047. }
  4048. static void perf_event_free_filter(struct perf_event *event)
  4049. {
  4050. ftrace_profile_free_filter(event);
  4051. }
  4052. #else
  4053. static inline void perf_tp_register(void)
  4054. {
  4055. }
  4056. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4057. {
  4058. return -ENOENT;
  4059. }
  4060. static void perf_event_free_filter(struct perf_event *event)
  4061. {
  4062. }
  4063. #endif /* CONFIG_EVENT_TRACING */
  4064. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4065. void perf_bp_event(struct perf_event *bp, void *data)
  4066. {
  4067. struct perf_sample_data sample;
  4068. struct pt_regs *regs = data;
  4069. perf_sample_data_init(&sample, bp->attr.bp_addr);
  4070. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  4071. perf_swevent_event(bp, 1, 1, &sample, regs);
  4072. }
  4073. #endif
  4074. /*
  4075. * hrtimer based swevent callback
  4076. */
  4077. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  4078. {
  4079. enum hrtimer_restart ret = HRTIMER_RESTART;
  4080. struct perf_sample_data data;
  4081. struct pt_regs *regs;
  4082. struct perf_event *event;
  4083. u64 period;
  4084. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  4085. event->pmu->read(event);
  4086. perf_sample_data_init(&data, 0);
  4087. data.period = event->hw.last_period;
  4088. regs = get_irq_regs();
  4089. if (regs && !perf_exclude_event(event, regs)) {
  4090. if (!(event->attr.exclude_idle && current->pid == 0))
  4091. if (perf_event_overflow(event, 0, &data, regs))
  4092. ret = HRTIMER_NORESTART;
  4093. }
  4094. period = max_t(u64, 10000, event->hw.sample_period);
  4095. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  4096. return ret;
  4097. }
  4098. static void perf_swevent_start_hrtimer(struct perf_event *event)
  4099. {
  4100. struct hw_perf_event *hwc = &event->hw;
  4101. s64 period;
  4102. if (!is_sampling_event(event))
  4103. return;
  4104. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  4105. hwc->hrtimer.function = perf_swevent_hrtimer;
  4106. period = local64_read(&hwc->period_left);
  4107. if (period) {
  4108. if (period < 0)
  4109. period = 10000;
  4110. local64_set(&hwc->period_left, 0);
  4111. } else {
  4112. period = max_t(u64, 10000, hwc->sample_period);
  4113. }
  4114. __hrtimer_start_range_ns(&hwc->hrtimer,
  4115. ns_to_ktime(period), 0,
  4116. HRTIMER_MODE_REL_PINNED, 0);
  4117. }
  4118. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  4119. {
  4120. struct hw_perf_event *hwc = &event->hw;
  4121. if (is_sampling_event(event)) {
  4122. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  4123. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  4124. hrtimer_cancel(&hwc->hrtimer);
  4125. }
  4126. }
  4127. /*
  4128. * Software event: cpu wall time clock
  4129. */
  4130. static void cpu_clock_event_update(struct perf_event *event)
  4131. {
  4132. s64 prev;
  4133. u64 now;
  4134. now = local_clock();
  4135. prev = local64_xchg(&event->hw.prev_count, now);
  4136. local64_add(now - prev, &event->count);
  4137. }
  4138. static void cpu_clock_event_start(struct perf_event *event, int flags)
  4139. {
  4140. local64_set(&event->hw.prev_count, local_clock());
  4141. perf_swevent_start_hrtimer(event);
  4142. }
  4143. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  4144. {
  4145. perf_swevent_cancel_hrtimer(event);
  4146. cpu_clock_event_update(event);
  4147. }
  4148. static int cpu_clock_event_add(struct perf_event *event, int flags)
  4149. {
  4150. if (flags & PERF_EF_START)
  4151. cpu_clock_event_start(event, flags);
  4152. return 0;
  4153. }
  4154. static void cpu_clock_event_del(struct perf_event *event, int flags)
  4155. {
  4156. cpu_clock_event_stop(event, flags);
  4157. }
  4158. static void cpu_clock_event_read(struct perf_event *event)
  4159. {
  4160. cpu_clock_event_update(event);
  4161. }
  4162. static int cpu_clock_event_init(struct perf_event *event)
  4163. {
  4164. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4165. return -ENOENT;
  4166. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  4167. return -ENOENT;
  4168. return 0;
  4169. }
  4170. static struct pmu perf_cpu_clock = {
  4171. .task_ctx_nr = perf_sw_context,
  4172. .event_init = cpu_clock_event_init,
  4173. .add = cpu_clock_event_add,
  4174. .del = cpu_clock_event_del,
  4175. .start = cpu_clock_event_start,
  4176. .stop = cpu_clock_event_stop,
  4177. .read = cpu_clock_event_read,
  4178. };
  4179. /*
  4180. * Software event: task time clock
  4181. */
  4182. static void task_clock_event_update(struct perf_event *event, u64 now)
  4183. {
  4184. u64 prev;
  4185. s64 delta;
  4186. prev = local64_xchg(&event->hw.prev_count, now);
  4187. delta = now - prev;
  4188. local64_add(delta, &event->count);
  4189. }
  4190. static void task_clock_event_start(struct perf_event *event, int flags)
  4191. {
  4192. local64_set(&event->hw.prev_count, event->ctx->time);
  4193. perf_swevent_start_hrtimer(event);
  4194. }
  4195. static void task_clock_event_stop(struct perf_event *event, int flags)
  4196. {
  4197. perf_swevent_cancel_hrtimer(event);
  4198. task_clock_event_update(event, event->ctx->time);
  4199. }
  4200. static int task_clock_event_add(struct perf_event *event, int flags)
  4201. {
  4202. if (flags & PERF_EF_START)
  4203. task_clock_event_start(event, flags);
  4204. return 0;
  4205. }
  4206. static void task_clock_event_del(struct perf_event *event, int flags)
  4207. {
  4208. task_clock_event_stop(event, PERF_EF_UPDATE);
  4209. }
  4210. static void task_clock_event_read(struct perf_event *event)
  4211. {
  4212. u64 time;
  4213. if (!in_nmi()) {
  4214. update_context_time(event->ctx);
  4215. time = event->ctx->time;
  4216. } else {
  4217. u64 now = perf_clock();
  4218. u64 delta = now - event->ctx->timestamp;
  4219. time = event->ctx->time + delta;
  4220. }
  4221. task_clock_event_update(event, time);
  4222. }
  4223. static int task_clock_event_init(struct perf_event *event)
  4224. {
  4225. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4226. return -ENOENT;
  4227. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  4228. return -ENOENT;
  4229. return 0;
  4230. }
  4231. static struct pmu perf_task_clock = {
  4232. .task_ctx_nr = perf_sw_context,
  4233. .event_init = task_clock_event_init,
  4234. .add = task_clock_event_add,
  4235. .del = task_clock_event_del,
  4236. .start = task_clock_event_start,
  4237. .stop = task_clock_event_stop,
  4238. .read = task_clock_event_read,
  4239. };
  4240. static void perf_pmu_nop_void(struct pmu *pmu)
  4241. {
  4242. }
  4243. static int perf_pmu_nop_int(struct pmu *pmu)
  4244. {
  4245. return 0;
  4246. }
  4247. static void perf_pmu_start_txn(struct pmu *pmu)
  4248. {
  4249. perf_pmu_disable(pmu);
  4250. }
  4251. static int perf_pmu_commit_txn(struct pmu *pmu)
  4252. {
  4253. perf_pmu_enable(pmu);
  4254. return 0;
  4255. }
  4256. static void perf_pmu_cancel_txn(struct pmu *pmu)
  4257. {
  4258. perf_pmu_enable(pmu);
  4259. }
  4260. /*
  4261. * Ensures all contexts with the same task_ctx_nr have the same
  4262. * pmu_cpu_context too.
  4263. */
  4264. static void *find_pmu_context(int ctxn)
  4265. {
  4266. struct pmu *pmu;
  4267. if (ctxn < 0)
  4268. return NULL;
  4269. list_for_each_entry(pmu, &pmus, entry) {
  4270. if (pmu->task_ctx_nr == ctxn)
  4271. return pmu->pmu_cpu_context;
  4272. }
  4273. return NULL;
  4274. }
  4275. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  4276. {
  4277. int cpu;
  4278. for_each_possible_cpu(cpu) {
  4279. struct perf_cpu_context *cpuctx;
  4280. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4281. if (cpuctx->active_pmu == old_pmu)
  4282. cpuctx->active_pmu = pmu;
  4283. }
  4284. }
  4285. static void free_pmu_context(struct pmu *pmu)
  4286. {
  4287. struct pmu *i;
  4288. mutex_lock(&pmus_lock);
  4289. /*
  4290. * Like a real lame refcount.
  4291. */
  4292. list_for_each_entry(i, &pmus, entry) {
  4293. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  4294. update_pmu_context(i, pmu);
  4295. goto out;
  4296. }
  4297. }
  4298. free_percpu(pmu->pmu_cpu_context);
  4299. out:
  4300. mutex_unlock(&pmus_lock);
  4301. }
  4302. static struct idr pmu_idr;
  4303. static ssize_t
  4304. type_show(struct device *dev, struct device_attribute *attr, char *page)
  4305. {
  4306. struct pmu *pmu = dev_get_drvdata(dev);
  4307. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  4308. }
  4309. static struct device_attribute pmu_dev_attrs[] = {
  4310. __ATTR_RO(type),
  4311. __ATTR_NULL,
  4312. };
  4313. static int pmu_bus_running;
  4314. static struct bus_type pmu_bus = {
  4315. .name = "event_source",
  4316. .dev_attrs = pmu_dev_attrs,
  4317. };
  4318. static void pmu_dev_release(struct device *dev)
  4319. {
  4320. kfree(dev);
  4321. }
  4322. static int pmu_dev_alloc(struct pmu *pmu)
  4323. {
  4324. int ret = -ENOMEM;
  4325. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  4326. if (!pmu->dev)
  4327. goto out;
  4328. device_initialize(pmu->dev);
  4329. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  4330. if (ret)
  4331. goto free_dev;
  4332. dev_set_drvdata(pmu->dev, pmu);
  4333. pmu->dev->bus = &pmu_bus;
  4334. pmu->dev->release = pmu_dev_release;
  4335. ret = device_add(pmu->dev);
  4336. if (ret)
  4337. goto free_dev;
  4338. out:
  4339. return ret;
  4340. free_dev:
  4341. put_device(pmu->dev);
  4342. goto out;
  4343. }
  4344. int perf_pmu_register(struct pmu *pmu, char *name, int type)
  4345. {
  4346. int cpu, ret;
  4347. mutex_lock(&pmus_lock);
  4348. ret = -ENOMEM;
  4349. pmu->pmu_disable_count = alloc_percpu(int);
  4350. if (!pmu->pmu_disable_count)
  4351. goto unlock;
  4352. pmu->type = -1;
  4353. if (!name)
  4354. goto skip_type;
  4355. pmu->name = name;
  4356. if (type < 0) {
  4357. int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
  4358. if (!err)
  4359. goto free_pdc;
  4360. err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
  4361. if (err) {
  4362. ret = err;
  4363. goto free_pdc;
  4364. }
  4365. }
  4366. pmu->type = type;
  4367. if (pmu_bus_running) {
  4368. ret = pmu_dev_alloc(pmu);
  4369. if (ret)
  4370. goto free_idr;
  4371. }
  4372. skip_type:
  4373. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  4374. if (pmu->pmu_cpu_context)
  4375. goto got_cpu_context;
  4376. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  4377. if (!pmu->pmu_cpu_context)
  4378. goto free_dev;
  4379. for_each_possible_cpu(cpu) {
  4380. struct perf_cpu_context *cpuctx;
  4381. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4382. __perf_event_init_context(&cpuctx->ctx);
  4383. cpuctx->ctx.type = cpu_context;
  4384. cpuctx->ctx.pmu = pmu;
  4385. cpuctx->jiffies_interval = 1;
  4386. INIT_LIST_HEAD(&cpuctx->rotation_list);
  4387. cpuctx->active_pmu = pmu;
  4388. }
  4389. got_cpu_context:
  4390. if (!pmu->start_txn) {
  4391. if (pmu->pmu_enable) {
  4392. /*
  4393. * If we have pmu_enable/pmu_disable calls, install
  4394. * transaction stubs that use that to try and batch
  4395. * hardware accesses.
  4396. */
  4397. pmu->start_txn = perf_pmu_start_txn;
  4398. pmu->commit_txn = perf_pmu_commit_txn;
  4399. pmu->cancel_txn = perf_pmu_cancel_txn;
  4400. } else {
  4401. pmu->start_txn = perf_pmu_nop_void;
  4402. pmu->commit_txn = perf_pmu_nop_int;
  4403. pmu->cancel_txn = perf_pmu_nop_void;
  4404. }
  4405. }
  4406. if (!pmu->pmu_enable) {
  4407. pmu->pmu_enable = perf_pmu_nop_void;
  4408. pmu->pmu_disable = perf_pmu_nop_void;
  4409. }
  4410. list_add_rcu(&pmu->entry, &pmus);
  4411. ret = 0;
  4412. unlock:
  4413. mutex_unlock(&pmus_lock);
  4414. return ret;
  4415. free_dev:
  4416. device_del(pmu->dev);
  4417. put_device(pmu->dev);
  4418. free_idr:
  4419. if (pmu->type >= PERF_TYPE_MAX)
  4420. idr_remove(&pmu_idr, pmu->type);
  4421. free_pdc:
  4422. free_percpu(pmu->pmu_disable_count);
  4423. goto unlock;
  4424. }
  4425. void perf_pmu_unregister(struct pmu *pmu)
  4426. {
  4427. mutex_lock(&pmus_lock);
  4428. list_del_rcu(&pmu->entry);
  4429. mutex_unlock(&pmus_lock);
  4430. /*
  4431. * We dereference the pmu list under both SRCU and regular RCU, so
  4432. * synchronize against both of those.
  4433. */
  4434. synchronize_srcu(&pmus_srcu);
  4435. synchronize_rcu();
  4436. free_percpu(pmu->pmu_disable_count);
  4437. if (pmu->type >= PERF_TYPE_MAX)
  4438. idr_remove(&pmu_idr, pmu->type);
  4439. device_del(pmu->dev);
  4440. put_device(pmu->dev);
  4441. free_pmu_context(pmu);
  4442. }
  4443. struct pmu *perf_init_event(struct perf_event *event)
  4444. {
  4445. struct pmu *pmu = NULL;
  4446. int idx;
  4447. idx = srcu_read_lock(&pmus_srcu);
  4448. rcu_read_lock();
  4449. pmu = idr_find(&pmu_idr, event->attr.type);
  4450. rcu_read_unlock();
  4451. if (pmu)
  4452. goto unlock;
  4453. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4454. int ret = pmu->event_init(event);
  4455. if (!ret)
  4456. goto unlock;
  4457. if (ret != -ENOENT) {
  4458. pmu = ERR_PTR(ret);
  4459. goto unlock;
  4460. }
  4461. }
  4462. pmu = ERR_PTR(-ENOENT);
  4463. unlock:
  4464. srcu_read_unlock(&pmus_srcu, idx);
  4465. return pmu;
  4466. }
  4467. /*
  4468. * Allocate and initialize a event structure
  4469. */
  4470. static struct perf_event *
  4471. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  4472. struct task_struct *task,
  4473. struct perf_event *group_leader,
  4474. struct perf_event *parent_event,
  4475. perf_overflow_handler_t overflow_handler)
  4476. {
  4477. struct pmu *pmu;
  4478. struct perf_event *event;
  4479. struct hw_perf_event *hwc;
  4480. long err;
  4481. if ((unsigned)cpu >= nr_cpu_ids) {
  4482. if (!task || cpu != -1)
  4483. return ERR_PTR(-EINVAL);
  4484. }
  4485. event = kzalloc(sizeof(*event), GFP_KERNEL);
  4486. if (!event)
  4487. return ERR_PTR(-ENOMEM);
  4488. /*
  4489. * Single events are their own group leaders, with an
  4490. * empty sibling list:
  4491. */
  4492. if (!group_leader)
  4493. group_leader = event;
  4494. mutex_init(&event->child_mutex);
  4495. INIT_LIST_HEAD(&event->child_list);
  4496. INIT_LIST_HEAD(&event->group_entry);
  4497. INIT_LIST_HEAD(&event->event_entry);
  4498. INIT_LIST_HEAD(&event->sibling_list);
  4499. init_waitqueue_head(&event->waitq);
  4500. init_irq_work(&event->pending, perf_pending_event);
  4501. mutex_init(&event->mmap_mutex);
  4502. event->cpu = cpu;
  4503. event->attr = *attr;
  4504. event->group_leader = group_leader;
  4505. event->pmu = NULL;
  4506. event->oncpu = -1;
  4507. event->parent = parent_event;
  4508. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  4509. event->id = atomic64_inc_return(&perf_event_id);
  4510. event->state = PERF_EVENT_STATE_INACTIVE;
  4511. if (task) {
  4512. event->attach_state = PERF_ATTACH_TASK;
  4513. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4514. /*
  4515. * hw_breakpoint is a bit difficult here..
  4516. */
  4517. if (attr->type == PERF_TYPE_BREAKPOINT)
  4518. event->hw.bp_target = task;
  4519. #endif
  4520. }
  4521. if (!overflow_handler && parent_event)
  4522. overflow_handler = parent_event->overflow_handler;
  4523. event->overflow_handler = overflow_handler;
  4524. if (attr->disabled)
  4525. event->state = PERF_EVENT_STATE_OFF;
  4526. pmu = NULL;
  4527. hwc = &event->hw;
  4528. hwc->sample_period = attr->sample_period;
  4529. if (attr->freq && attr->sample_freq)
  4530. hwc->sample_period = 1;
  4531. hwc->last_period = hwc->sample_period;
  4532. local64_set(&hwc->period_left, hwc->sample_period);
  4533. /*
  4534. * we currently do not support PERF_FORMAT_GROUP on inherited events
  4535. */
  4536. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  4537. goto done;
  4538. pmu = perf_init_event(event);
  4539. done:
  4540. err = 0;
  4541. if (!pmu)
  4542. err = -EINVAL;
  4543. else if (IS_ERR(pmu))
  4544. err = PTR_ERR(pmu);
  4545. if (err) {
  4546. if (event->ns)
  4547. put_pid_ns(event->ns);
  4548. kfree(event);
  4549. return ERR_PTR(err);
  4550. }
  4551. event->pmu = pmu;
  4552. if (!event->parent) {
  4553. if (event->attach_state & PERF_ATTACH_TASK)
  4554. jump_label_inc(&perf_task_events);
  4555. if (event->attr.mmap || event->attr.mmap_data)
  4556. atomic_inc(&nr_mmap_events);
  4557. if (event->attr.comm)
  4558. atomic_inc(&nr_comm_events);
  4559. if (event->attr.task)
  4560. atomic_inc(&nr_task_events);
  4561. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  4562. err = get_callchain_buffers();
  4563. if (err) {
  4564. free_event(event);
  4565. return ERR_PTR(err);
  4566. }
  4567. }
  4568. }
  4569. return event;
  4570. }
  4571. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4572. struct perf_event_attr *attr)
  4573. {
  4574. u32 size;
  4575. int ret;
  4576. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  4577. return -EFAULT;
  4578. /*
  4579. * zero the full structure, so that a short copy will be nice.
  4580. */
  4581. memset(attr, 0, sizeof(*attr));
  4582. ret = get_user(size, &uattr->size);
  4583. if (ret)
  4584. return ret;
  4585. if (size > PAGE_SIZE) /* silly large */
  4586. goto err_size;
  4587. if (!size) /* abi compat */
  4588. size = PERF_ATTR_SIZE_VER0;
  4589. if (size < PERF_ATTR_SIZE_VER0)
  4590. goto err_size;
  4591. /*
  4592. * If we're handed a bigger struct than we know of,
  4593. * ensure all the unknown bits are 0 - i.e. new
  4594. * user-space does not rely on any kernel feature
  4595. * extensions we dont know about yet.
  4596. */
  4597. if (size > sizeof(*attr)) {
  4598. unsigned char __user *addr;
  4599. unsigned char __user *end;
  4600. unsigned char val;
  4601. addr = (void __user *)uattr + sizeof(*attr);
  4602. end = (void __user *)uattr + size;
  4603. for (; addr < end; addr++) {
  4604. ret = get_user(val, addr);
  4605. if (ret)
  4606. return ret;
  4607. if (val)
  4608. goto err_size;
  4609. }
  4610. size = sizeof(*attr);
  4611. }
  4612. ret = copy_from_user(attr, uattr, size);
  4613. if (ret)
  4614. return -EFAULT;
  4615. /*
  4616. * If the type exists, the corresponding creation will verify
  4617. * the attr->config.
  4618. */
  4619. if (attr->type >= PERF_TYPE_MAX)
  4620. return -EINVAL;
  4621. if (attr->__reserved_1)
  4622. return -EINVAL;
  4623. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4624. return -EINVAL;
  4625. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4626. return -EINVAL;
  4627. out:
  4628. return ret;
  4629. err_size:
  4630. put_user(sizeof(*attr), &uattr->size);
  4631. ret = -E2BIG;
  4632. goto out;
  4633. }
  4634. static int
  4635. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4636. {
  4637. struct perf_buffer *buffer = NULL, *old_buffer = NULL;
  4638. int ret = -EINVAL;
  4639. if (!output_event)
  4640. goto set;
  4641. /* don't allow circular references */
  4642. if (event == output_event)
  4643. goto out;
  4644. /*
  4645. * Don't allow cross-cpu buffers
  4646. */
  4647. if (output_event->cpu != event->cpu)
  4648. goto out;
  4649. /*
  4650. * If its not a per-cpu buffer, it must be the same task.
  4651. */
  4652. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4653. goto out;
  4654. set:
  4655. mutex_lock(&event->mmap_mutex);
  4656. /* Can't redirect output if we've got an active mmap() */
  4657. if (atomic_read(&event->mmap_count))
  4658. goto unlock;
  4659. if (output_event) {
  4660. /* get the buffer we want to redirect to */
  4661. buffer = perf_buffer_get(output_event);
  4662. if (!buffer)
  4663. goto unlock;
  4664. }
  4665. old_buffer = event->buffer;
  4666. rcu_assign_pointer(event->buffer, buffer);
  4667. ret = 0;
  4668. unlock:
  4669. mutex_unlock(&event->mmap_mutex);
  4670. if (old_buffer)
  4671. perf_buffer_put(old_buffer);
  4672. out:
  4673. return ret;
  4674. }
  4675. /**
  4676. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4677. *
  4678. * @attr_uptr: event_id type attributes for monitoring/sampling
  4679. * @pid: target pid
  4680. * @cpu: target cpu
  4681. * @group_fd: group leader event fd
  4682. */
  4683. SYSCALL_DEFINE5(perf_event_open,
  4684. struct perf_event_attr __user *, attr_uptr,
  4685. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4686. {
  4687. struct perf_event *group_leader = NULL, *output_event = NULL;
  4688. struct perf_event *event, *sibling;
  4689. struct perf_event_attr attr;
  4690. struct perf_event_context *ctx;
  4691. struct file *event_file = NULL;
  4692. struct file *group_file = NULL;
  4693. struct task_struct *task = NULL;
  4694. struct pmu *pmu;
  4695. int event_fd;
  4696. int move_group = 0;
  4697. int fput_needed = 0;
  4698. int err;
  4699. /* for future expandability... */
  4700. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  4701. return -EINVAL;
  4702. err = perf_copy_attr(attr_uptr, &attr);
  4703. if (err)
  4704. return err;
  4705. if (!attr.exclude_kernel) {
  4706. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4707. return -EACCES;
  4708. }
  4709. if (attr.freq) {
  4710. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4711. return -EINVAL;
  4712. }
  4713. event_fd = get_unused_fd_flags(O_RDWR);
  4714. if (event_fd < 0)
  4715. return event_fd;
  4716. if (group_fd != -1) {
  4717. group_leader = perf_fget_light(group_fd, &fput_needed);
  4718. if (IS_ERR(group_leader)) {
  4719. err = PTR_ERR(group_leader);
  4720. goto err_fd;
  4721. }
  4722. group_file = group_leader->filp;
  4723. if (flags & PERF_FLAG_FD_OUTPUT)
  4724. output_event = group_leader;
  4725. if (flags & PERF_FLAG_FD_NO_GROUP)
  4726. group_leader = NULL;
  4727. }
  4728. if (pid != -1) {
  4729. task = find_lively_task_by_vpid(pid);
  4730. if (IS_ERR(task)) {
  4731. err = PTR_ERR(task);
  4732. goto err_group_fd;
  4733. }
  4734. }
  4735. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
  4736. if (IS_ERR(event)) {
  4737. err = PTR_ERR(event);
  4738. goto err_task;
  4739. }
  4740. /*
  4741. * Special case software events and allow them to be part of
  4742. * any hardware group.
  4743. */
  4744. pmu = event->pmu;
  4745. if (group_leader &&
  4746. (is_software_event(event) != is_software_event(group_leader))) {
  4747. if (is_software_event(event)) {
  4748. /*
  4749. * If event and group_leader are not both a software
  4750. * event, and event is, then group leader is not.
  4751. *
  4752. * Allow the addition of software events to !software
  4753. * groups, this is safe because software events never
  4754. * fail to schedule.
  4755. */
  4756. pmu = group_leader->pmu;
  4757. } else if (is_software_event(group_leader) &&
  4758. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  4759. /*
  4760. * In case the group is a pure software group, and we
  4761. * try to add a hardware event, move the whole group to
  4762. * the hardware context.
  4763. */
  4764. move_group = 1;
  4765. }
  4766. }
  4767. /*
  4768. * Get the target context (task or percpu):
  4769. */
  4770. ctx = find_get_context(pmu, task, cpu);
  4771. if (IS_ERR(ctx)) {
  4772. err = PTR_ERR(ctx);
  4773. goto err_alloc;
  4774. }
  4775. /*
  4776. * Look up the group leader (we will attach this event to it):
  4777. */
  4778. if (group_leader) {
  4779. err = -EINVAL;
  4780. /*
  4781. * Do not allow a recursive hierarchy (this new sibling
  4782. * becoming part of another group-sibling):
  4783. */
  4784. if (group_leader->group_leader != group_leader)
  4785. goto err_context;
  4786. /*
  4787. * Do not allow to attach to a group in a different
  4788. * task or CPU context:
  4789. */
  4790. if (move_group) {
  4791. if (group_leader->ctx->type != ctx->type)
  4792. goto err_context;
  4793. } else {
  4794. if (group_leader->ctx != ctx)
  4795. goto err_context;
  4796. }
  4797. /*
  4798. * Only a group leader can be exclusive or pinned
  4799. */
  4800. if (attr.exclusive || attr.pinned)
  4801. goto err_context;
  4802. }
  4803. if (output_event) {
  4804. err = perf_event_set_output(event, output_event);
  4805. if (err)
  4806. goto err_context;
  4807. }
  4808. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  4809. if (IS_ERR(event_file)) {
  4810. err = PTR_ERR(event_file);
  4811. goto err_context;
  4812. }
  4813. if (move_group) {
  4814. struct perf_event_context *gctx = group_leader->ctx;
  4815. mutex_lock(&gctx->mutex);
  4816. perf_event_remove_from_context(group_leader);
  4817. list_for_each_entry(sibling, &group_leader->sibling_list,
  4818. group_entry) {
  4819. perf_event_remove_from_context(sibling);
  4820. put_ctx(gctx);
  4821. }
  4822. mutex_unlock(&gctx->mutex);
  4823. put_ctx(gctx);
  4824. }
  4825. event->filp = event_file;
  4826. WARN_ON_ONCE(ctx->parent_ctx);
  4827. mutex_lock(&ctx->mutex);
  4828. if (move_group) {
  4829. perf_install_in_context(ctx, group_leader, cpu);
  4830. get_ctx(ctx);
  4831. list_for_each_entry(sibling, &group_leader->sibling_list,
  4832. group_entry) {
  4833. perf_install_in_context(ctx, sibling, cpu);
  4834. get_ctx(ctx);
  4835. }
  4836. }
  4837. perf_install_in_context(ctx, event, cpu);
  4838. ++ctx->generation;
  4839. mutex_unlock(&ctx->mutex);
  4840. event->owner = current;
  4841. mutex_lock(&current->perf_event_mutex);
  4842. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4843. mutex_unlock(&current->perf_event_mutex);
  4844. /*
  4845. * Precalculate sample_data sizes
  4846. */
  4847. perf_event__header_size(event);
  4848. perf_event__id_header_size(event);
  4849. /*
  4850. * Drop the reference on the group_event after placing the
  4851. * new event on the sibling_list. This ensures destruction
  4852. * of the group leader will find the pointer to itself in
  4853. * perf_group_detach().
  4854. */
  4855. fput_light(group_file, fput_needed);
  4856. fd_install(event_fd, event_file);
  4857. return event_fd;
  4858. err_context:
  4859. put_ctx(ctx);
  4860. err_alloc:
  4861. free_event(event);
  4862. err_task:
  4863. if (task)
  4864. put_task_struct(task);
  4865. err_group_fd:
  4866. fput_light(group_file, fput_needed);
  4867. err_fd:
  4868. put_unused_fd(event_fd);
  4869. return err;
  4870. }
  4871. /**
  4872. * perf_event_create_kernel_counter
  4873. *
  4874. * @attr: attributes of the counter to create
  4875. * @cpu: cpu in which the counter is bound
  4876. * @task: task to profile (NULL for percpu)
  4877. */
  4878. struct perf_event *
  4879. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  4880. struct task_struct *task,
  4881. perf_overflow_handler_t overflow_handler)
  4882. {
  4883. struct perf_event_context *ctx;
  4884. struct perf_event *event;
  4885. int err;
  4886. /*
  4887. * Get the target context (task or percpu):
  4888. */
  4889. event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
  4890. if (IS_ERR(event)) {
  4891. err = PTR_ERR(event);
  4892. goto err;
  4893. }
  4894. ctx = find_get_context(event->pmu, task, cpu);
  4895. if (IS_ERR(ctx)) {
  4896. err = PTR_ERR(ctx);
  4897. goto err_free;
  4898. }
  4899. event->filp = NULL;
  4900. WARN_ON_ONCE(ctx->parent_ctx);
  4901. mutex_lock(&ctx->mutex);
  4902. perf_install_in_context(ctx, event, cpu);
  4903. ++ctx->generation;
  4904. mutex_unlock(&ctx->mutex);
  4905. return event;
  4906. err_free:
  4907. free_event(event);
  4908. err:
  4909. return ERR_PTR(err);
  4910. }
  4911. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  4912. static void sync_child_event(struct perf_event *child_event,
  4913. struct task_struct *child)
  4914. {
  4915. struct perf_event *parent_event = child_event->parent;
  4916. u64 child_val;
  4917. if (child_event->attr.inherit_stat)
  4918. perf_event_read_event(child_event, child);
  4919. child_val = perf_event_count(child_event);
  4920. /*
  4921. * Add back the child's count to the parent's count:
  4922. */
  4923. atomic64_add(child_val, &parent_event->child_count);
  4924. atomic64_add(child_event->total_time_enabled,
  4925. &parent_event->child_total_time_enabled);
  4926. atomic64_add(child_event->total_time_running,
  4927. &parent_event->child_total_time_running);
  4928. /*
  4929. * Remove this event from the parent's list
  4930. */
  4931. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4932. mutex_lock(&parent_event->child_mutex);
  4933. list_del_init(&child_event->child_list);
  4934. mutex_unlock(&parent_event->child_mutex);
  4935. /*
  4936. * Release the parent event, if this was the last
  4937. * reference to it.
  4938. */
  4939. fput(parent_event->filp);
  4940. }
  4941. static void
  4942. __perf_event_exit_task(struct perf_event *child_event,
  4943. struct perf_event_context *child_ctx,
  4944. struct task_struct *child)
  4945. {
  4946. struct perf_event *parent_event;
  4947. perf_event_remove_from_context(child_event);
  4948. parent_event = child_event->parent;
  4949. /*
  4950. * It can happen that parent exits first, and has events
  4951. * that are still around due to the child reference. These
  4952. * events need to be zapped - but otherwise linger.
  4953. */
  4954. if (parent_event) {
  4955. sync_child_event(child_event, child);
  4956. free_event(child_event);
  4957. }
  4958. }
  4959. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  4960. {
  4961. struct perf_event *child_event, *tmp;
  4962. struct perf_event_context *child_ctx;
  4963. unsigned long flags;
  4964. if (likely(!child->perf_event_ctxp[ctxn])) {
  4965. perf_event_task(child, NULL, 0);
  4966. return;
  4967. }
  4968. local_irq_save(flags);
  4969. /*
  4970. * We can't reschedule here because interrupts are disabled,
  4971. * and either child is current or it is a task that can't be
  4972. * scheduled, so we are now safe from rescheduling changing
  4973. * our context.
  4974. */
  4975. child_ctx = rcu_dereference(child->perf_event_ctxp[ctxn]);
  4976. task_ctx_sched_out(child_ctx, EVENT_ALL);
  4977. /*
  4978. * Take the context lock here so that if find_get_context is
  4979. * reading child->perf_event_ctxp, we wait until it has
  4980. * incremented the context's refcount before we do put_ctx below.
  4981. */
  4982. raw_spin_lock(&child_ctx->lock);
  4983. child->perf_event_ctxp[ctxn] = NULL;
  4984. /*
  4985. * If this context is a clone; unclone it so it can't get
  4986. * swapped to another process while we're removing all
  4987. * the events from it.
  4988. */
  4989. unclone_ctx(child_ctx);
  4990. update_context_time(child_ctx);
  4991. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4992. /*
  4993. * Report the task dead after unscheduling the events so that we
  4994. * won't get any samples after PERF_RECORD_EXIT. We can however still
  4995. * get a few PERF_RECORD_READ events.
  4996. */
  4997. perf_event_task(child, child_ctx, 0);
  4998. /*
  4999. * We can recurse on the same lock type through:
  5000. *
  5001. * __perf_event_exit_task()
  5002. * sync_child_event()
  5003. * fput(parent_event->filp)
  5004. * perf_release()
  5005. * mutex_lock(&ctx->mutex)
  5006. *
  5007. * But since its the parent context it won't be the same instance.
  5008. */
  5009. mutex_lock(&child_ctx->mutex);
  5010. again:
  5011. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  5012. group_entry)
  5013. __perf_event_exit_task(child_event, child_ctx, child);
  5014. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  5015. group_entry)
  5016. __perf_event_exit_task(child_event, child_ctx, child);
  5017. /*
  5018. * If the last event was a group event, it will have appended all
  5019. * its siblings to the list, but we obtained 'tmp' before that which
  5020. * will still point to the list head terminating the iteration.
  5021. */
  5022. if (!list_empty(&child_ctx->pinned_groups) ||
  5023. !list_empty(&child_ctx->flexible_groups))
  5024. goto again;
  5025. mutex_unlock(&child_ctx->mutex);
  5026. put_ctx(child_ctx);
  5027. }
  5028. /*
  5029. * When a child task exits, feed back event values to parent events.
  5030. */
  5031. void perf_event_exit_task(struct task_struct *child)
  5032. {
  5033. struct perf_event *event, *tmp;
  5034. int ctxn;
  5035. mutex_lock(&child->perf_event_mutex);
  5036. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  5037. owner_entry) {
  5038. list_del_init(&event->owner_entry);
  5039. /*
  5040. * Ensure the list deletion is visible before we clear
  5041. * the owner, closes a race against perf_release() where
  5042. * we need to serialize on the owner->perf_event_mutex.
  5043. */
  5044. smp_wmb();
  5045. event->owner = NULL;
  5046. }
  5047. mutex_unlock(&child->perf_event_mutex);
  5048. for_each_task_context_nr(ctxn)
  5049. perf_event_exit_task_context(child, ctxn);
  5050. }
  5051. static void perf_free_event(struct perf_event *event,
  5052. struct perf_event_context *ctx)
  5053. {
  5054. struct perf_event *parent = event->parent;
  5055. if (WARN_ON_ONCE(!parent))
  5056. return;
  5057. mutex_lock(&parent->child_mutex);
  5058. list_del_init(&event->child_list);
  5059. mutex_unlock(&parent->child_mutex);
  5060. fput(parent->filp);
  5061. perf_group_detach(event);
  5062. list_del_event(event, ctx);
  5063. free_event(event);
  5064. }
  5065. /*
  5066. * free an unexposed, unused context as created by inheritance by
  5067. * perf_event_init_task below, used by fork() in case of fail.
  5068. */
  5069. void perf_event_free_task(struct task_struct *task)
  5070. {
  5071. struct perf_event_context *ctx;
  5072. struct perf_event *event, *tmp;
  5073. int ctxn;
  5074. for_each_task_context_nr(ctxn) {
  5075. ctx = task->perf_event_ctxp[ctxn];
  5076. if (!ctx)
  5077. continue;
  5078. mutex_lock(&ctx->mutex);
  5079. again:
  5080. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  5081. group_entry)
  5082. perf_free_event(event, ctx);
  5083. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  5084. group_entry)
  5085. perf_free_event(event, ctx);
  5086. if (!list_empty(&ctx->pinned_groups) ||
  5087. !list_empty(&ctx->flexible_groups))
  5088. goto again;
  5089. mutex_unlock(&ctx->mutex);
  5090. put_ctx(ctx);
  5091. }
  5092. }
  5093. void perf_event_delayed_put(struct task_struct *task)
  5094. {
  5095. int ctxn;
  5096. for_each_task_context_nr(ctxn)
  5097. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  5098. }
  5099. /*
  5100. * inherit a event from parent task to child task:
  5101. */
  5102. static struct perf_event *
  5103. inherit_event(struct perf_event *parent_event,
  5104. struct task_struct *parent,
  5105. struct perf_event_context *parent_ctx,
  5106. struct task_struct *child,
  5107. struct perf_event *group_leader,
  5108. struct perf_event_context *child_ctx)
  5109. {
  5110. struct perf_event *child_event;
  5111. unsigned long flags;
  5112. /*
  5113. * Instead of creating recursive hierarchies of events,
  5114. * we link inherited events back to the original parent,
  5115. * which has a filp for sure, which we use as the reference
  5116. * count:
  5117. */
  5118. if (parent_event->parent)
  5119. parent_event = parent_event->parent;
  5120. child_event = perf_event_alloc(&parent_event->attr,
  5121. parent_event->cpu,
  5122. child,
  5123. group_leader, parent_event,
  5124. NULL);
  5125. if (IS_ERR(child_event))
  5126. return child_event;
  5127. get_ctx(child_ctx);
  5128. /*
  5129. * Make the child state follow the state of the parent event,
  5130. * not its attr.disabled bit. We hold the parent's mutex,
  5131. * so we won't race with perf_event_{en, dis}able_family.
  5132. */
  5133. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  5134. child_event->state = PERF_EVENT_STATE_INACTIVE;
  5135. else
  5136. child_event->state = PERF_EVENT_STATE_OFF;
  5137. if (parent_event->attr.freq) {
  5138. u64 sample_period = parent_event->hw.sample_period;
  5139. struct hw_perf_event *hwc = &child_event->hw;
  5140. hwc->sample_period = sample_period;
  5141. hwc->last_period = sample_period;
  5142. local64_set(&hwc->period_left, sample_period);
  5143. }
  5144. child_event->ctx = child_ctx;
  5145. child_event->overflow_handler = parent_event->overflow_handler;
  5146. /*
  5147. * Precalculate sample_data sizes
  5148. */
  5149. perf_event__header_size(child_event);
  5150. perf_event__id_header_size(child_event);
  5151. /*
  5152. * Link it up in the child's context:
  5153. */
  5154. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  5155. add_event_to_ctx(child_event, child_ctx);
  5156. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  5157. /*
  5158. * Get a reference to the parent filp - we will fput it
  5159. * when the child event exits. This is safe to do because
  5160. * we are in the parent and we know that the filp still
  5161. * exists and has a nonzero count:
  5162. */
  5163. atomic_long_inc(&parent_event->filp->f_count);
  5164. /*
  5165. * Link this into the parent event's child list
  5166. */
  5167. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  5168. mutex_lock(&parent_event->child_mutex);
  5169. list_add_tail(&child_event->child_list, &parent_event->child_list);
  5170. mutex_unlock(&parent_event->child_mutex);
  5171. return child_event;
  5172. }
  5173. static int inherit_group(struct perf_event *parent_event,
  5174. struct task_struct *parent,
  5175. struct perf_event_context *parent_ctx,
  5176. struct task_struct *child,
  5177. struct perf_event_context *child_ctx)
  5178. {
  5179. struct perf_event *leader;
  5180. struct perf_event *sub;
  5181. struct perf_event *child_ctr;
  5182. leader = inherit_event(parent_event, parent, parent_ctx,
  5183. child, NULL, child_ctx);
  5184. if (IS_ERR(leader))
  5185. return PTR_ERR(leader);
  5186. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  5187. child_ctr = inherit_event(sub, parent, parent_ctx,
  5188. child, leader, child_ctx);
  5189. if (IS_ERR(child_ctr))
  5190. return PTR_ERR(child_ctr);
  5191. }
  5192. return 0;
  5193. }
  5194. static int
  5195. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  5196. struct perf_event_context *parent_ctx,
  5197. struct task_struct *child, int ctxn,
  5198. int *inherited_all)
  5199. {
  5200. int ret;
  5201. struct perf_event_context *child_ctx;
  5202. if (!event->attr.inherit) {
  5203. *inherited_all = 0;
  5204. return 0;
  5205. }
  5206. child_ctx = child->perf_event_ctxp[ctxn];
  5207. if (!child_ctx) {
  5208. /*
  5209. * This is executed from the parent task context, so
  5210. * inherit events that have been marked for cloning.
  5211. * First allocate and initialize a context for the
  5212. * child.
  5213. */
  5214. child_ctx = alloc_perf_context(event->pmu, child);
  5215. if (!child_ctx)
  5216. return -ENOMEM;
  5217. child->perf_event_ctxp[ctxn] = child_ctx;
  5218. }
  5219. ret = inherit_group(event, parent, parent_ctx,
  5220. child, child_ctx);
  5221. if (ret)
  5222. *inherited_all = 0;
  5223. return ret;
  5224. }
  5225. /*
  5226. * Initialize the perf_event context in task_struct
  5227. */
  5228. int perf_event_init_context(struct task_struct *child, int ctxn)
  5229. {
  5230. struct perf_event_context *child_ctx, *parent_ctx;
  5231. struct perf_event_context *cloned_ctx;
  5232. struct perf_event *event;
  5233. struct task_struct *parent = current;
  5234. int inherited_all = 1;
  5235. unsigned long flags;
  5236. int ret = 0;
  5237. child->perf_event_ctxp[ctxn] = NULL;
  5238. mutex_init(&child->perf_event_mutex);
  5239. INIT_LIST_HEAD(&child->perf_event_list);
  5240. if (likely(!parent->perf_event_ctxp[ctxn]))
  5241. return 0;
  5242. /*
  5243. * If the parent's context is a clone, pin it so it won't get
  5244. * swapped under us.
  5245. */
  5246. parent_ctx = perf_pin_task_context(parent, ctxn);
  5247. /*
  5248. * No need to check if parent_ctx != NULL here; since we saw
  5249. * it non-NULL earlier, the only reason for it to become NULL
  5250. * is if we exit, and since we're currently in the middle of
  5251. * a fork we can't be exiting at the same time.
  5252. */
  5253. /*
  5254. * Lock the parent list. No need to lock the child - not PID
  5255. * hashed yet and not running, so nobody can access it.
  5256. */
  5257. mutex_lock(&parent_ctx->mutex);
  5258. /*
  5259. * We dont have to disable NMIs - we are only looking at
  5260. * the list, not manipulating it:
  5261. */
  5262. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  5263. ret = inherit_task_group(event, parent, parent_ctx,
  5264. child, ctxn, &inherited_all);
  5265. if (ret)
  5266. break;
  5267. }
  5268. /*
  5269. * We can't hold ctx->lock when iterating the ->flexible_group list due
  5270. * to allocations, but we need to prevent rotation because
  5271. * rotate_ctx() will change the list from interrupt context.
  5272. */
  5273. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5274. parent_ctx->rotate_disable = 1;
  5275. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5276. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  5277. ret = inherit_task_group(event, parent, parent_ctx,
  5278. child, ctxn, &inherited_all);
  5279. if (ret)
  5280. break;
  5281. }
  5282. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  5283. parent_ctx->rotate_disable = 0;
  5284. child_ctx = child->perf_event_ctxp[ctxn];
  5285. if (child_ctx && inherited_all) {
  5286. /*
  5287. * Mark the child context as a clone of the parent
  5288. * context, or of whatever the parent is a clone of.
  5289. *
  5290. * Note that if the parent is a clone, the holding of
  5291. * parent_ctx->lock avoids it from being uncloned.
  5292. */
  5293. cloned_ctx = parent_ctx->parent_ctx;
  5294. if (cloned_ctx) {
  5295. child_ctx->parent_ctx = cloned_ctx;
  5296. child_ctx->parent_gen = parent_ctx->parent_gen;
  5297. } else {
  5298. child_ctx->parent_ctx = parent_ctx;
  5299. child_ctx->parent_gen = parent_ctx->generation;
  5300. }
  5301. get_ctx(child_ctx->parent_ctx);
  5302. }
  5303. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  5304. mutex_unlock(&parent_ctx->mutex);
  5305. perf_unpin_context(parent_ctx);
  5306. return ret;
  5307. }
  5308. /*
  5309. * Initialize the perf_event context in task_struct
  5310. */
  5311. int perf_event_init_task(struct task_struct *child)
  5312. {
  5313. int ctxn, ret;
  5314. for_each_task_context_nr(ctxn) {
  5315. ret = perf_event_init_context(child, ctxn);
  5316. if (ret)
  5317. return ret;
  5318. }
  5319. return 0;
  5320. }
  5321. static void __init perf_event_init_all_cpus(void)
  5322. {
  5323. struct swevent_htable *swhash;
  5324. int cpu;
  5325. for_each_possible_cpu(cpu) {
  5326. swhash = &per_cpu(swevent_htable, cpu);
  5327. mutex_init(&swhash->hlist_mutex);
  5328. INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
  5329. }
  5330. }
  5331. static void __cpuinit perf_event_init_cpu(int cpu)
  5332. {
  5333. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5334. mutex_lock(&swhash->hlist_mutex);
  5335. if (swhash->hlist_refcount > 0) {
  5336. struct swevent_hlist *hlist;
  5337. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  5338. WARN_ON(!hlist);
  5339. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5340. }
  5341. mutex_unlock(&swhash->hlist_mutex);
  5342. }
  5343. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
  5344. static void perf_pmu_rotate_stop(struct pmu *pmu)
  5345. {
  5346. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  5347. WARN_ON(!irqs_disabled());
  5348. list_del_init(&cpuctx->rotation_list);
  5349. }
  5350. static void __perf_event_exit_context(void *__info)
  5351. {
  5352. struct perf_event_context *ctx = __info;
  5353. struct perf_event *event, *tmp;
  5354. perf_pmu_rotate_stop(ctx->pmu);
  5355. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  5356. __perf_event_remove_from_context(event);
  5357. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  5358. __perf_event_remove_from_context(event);
  5359. }
  5360. static void perf_event_exit_cpu_context(int cpu)
  5361. {
  5362. struct perf_event_context *ctx;
  5363. struct pmu *pmu;
  5364. int idx;
  5365. idx = srcu_read_lock(&pmus_srcu);
  5366. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5367. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  5368. mutex_lock(&ctx->mutex);
  5369. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  5370. mutex_unlock(&ctx->mutex);
  5371. }
  5372. srcu_read_unlock(&pmus_srcu, idx);
  5373. }
  5374. static void perf_event_exit_cpu(int cpu)
  5375. {
  5376. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5377. mutex_lock(&swhash->hlist_mutex);
  5378. swevent_hlist_release(swhash);
  5379. mutex_unlock(&swhash->hlist_mutex);
  5380. perf_event_exit_cpu_context(cpu);
  5381. }
  5382. #else
  5383. static inline void perf_event_exit_cpu(int cpu) { }
  5384. #endif
  5385. static int
  5386. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  5387. {
  5388. int cpu;
  5389. for_each_online_cpu(cpu)
  5390. perf_event_exit_cpu(cpu);
  5391. return NOTIFY_OK;
  5392. }
  5393. /*
  5394. * Run the perf reboot notifier at the very last possible moment so that
  5395. * the generic watchdog code runs as long as possible.
  5396. */
  5397. static struct notifier_block perf_reboot_notifier = {
  5398. .notifier_call = perf_reboot,
  5399. .priority = INT_MIN,
  5400. };
  5401. static int __cpuinit
  5402. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  5403. {
  5404. unsigned int cpu = (long)hcpu;
  5405. switch (action & ~CPU_TASKS_FROZEN) {
  5406. case CPU_UP_PREPARE:
  5407. case CPU_DOWN_FAILED:
  5408. perf_event_init_cpu(cpu);
  5409. break;
  5410. case CPU_UP_CANCELED:
  5411. case CPU_DOWN_PREPARE:
  5412. perf_event_exit_cpu(cpu);
  5413. break;
  5414. default:
  5415. break;
  5416. }
  5417. return NOTIFY_OK;
  5418. }
  5419. void __init perf_event_init(void)
  5420. {
  5421. int ret;
  5422. idr_init(&pmu_idr);
  5423. perf_event_init_all_cpus();
  5424. init_srcu_struct(&pmus_srcu);
  5425. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  5426. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  5427. perf_pmu_register(&perf_task_clock, NULL, -1);
  5428. perf_tp_register();
  5429. perf_cpu_notifier(perf_cpu_notify);
  5430. register_reboot_notifier(&perf_reboot_notifier);
  5431. ret = init_hw_breakpoint();
  5432. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  5433. }
  5434. static int __init perf_event_sysfs_init(void)
  5435. {
  5436. struct pmu *pmu;
  5437. int ret;
  5438. mutex_lock(&pmus_lock);
  5439. ret = bus_register(&pmu_bus);
  5440. if (ret)
  5441. goto unlock;
  5442. list_for_each_entry(pmu, &pmus, entry) {
  5443. if (!pmu->name || pmu->type < 0)
  5444. continue;
  5445. ret = pmu_dev_alloc(pmu);
  5446. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  5447. }
  5448. pmu_bus_running = 1;
  5449. ret = 0;
  5450. unlock:
  5451. mutex_unlock(&pmus_lock);
  5452. return ret;
  5453. }
  5454. device_initcall(perf_event_sysfs_init);