core.c 161 KB

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