core.c 169 KB

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