core.c 160 KB

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