perf_event.c 143 KB

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