perf_event.c 143 KB

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