core.c 164 KB

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