core.c 211 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103
  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/tick.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/dcache.h>
  23. #include <linux/percpu.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/reboot.h>
  26. #include <linux/vmstat.h>
  27. #include <linux/device.h>
  28. #include <linux/export.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/rculist.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/anon_inodes.h>
  35. #include <linux/kernel_stat.h>
  36. #include <linux/cgroup.h>
  37. #include <linux/perf_event.h>
  38. #include <linux/trace_events.h>
  39. #include <linux/hw_breakpoint.h>
  40. #include <linux/mm_types.h>
  41. #include <linux/module.h>
  42. #include <linux/mman.h>
  43. #include <linux/compat.h>
  44. #include <linux/bpf.h>
  45. #include <linux/filter.h>
  46. #include "internal.h"
  47. #include <asm/irq_regs.h>
  48. static struct workqueue_struct *perf_wq;
  49. typedef int (*remote_function_f)(void *);
  50. struct remote_function_call {
  51. struct task_struct *p;
  52. remote_function_f func;
  53. void *info;
  54. int ret;
  55. };
  56. static void remote_function(void *data)
  57. {
  58. struct remote_function_call *tfc = data;
  59. struct task_struct *p = tfc->p;
  60. if (p) {
  61. tfc->ret = -EAGAIN;
  62. if (task_cpu(p) != smp_processor_id() || !task_curr(p))
  63. return;
  64. }
  65. tfc->ret = tfc->func(tfc->info);
  66. }
  67. /**
  68. * task_function_call - call a function on the cpu on which a task runs
  69. * @p: the task to evaluate
  70. * @func: the function to be called
  71. * @info: the function call argument
  72. *
  73. * Calls the function @func when the task is currently running. This might
  74. * be on the current CPU, which just calls the function directly
  75. *
  76. * returns: @func return value, or
  77. * -ESRCH - when the process isn't running
  78. * -EAGAIN - when the process moved away
  79. */
  80. static int
  81. task_function_call(struct task_struct *p, remote_function_f func, void *info)
  82. {
  83. struct remote_function_call data = {
  84. .p = p,
  85. .func = func,
  86. .info = info,
  87. .ret = -ESRCH, /* No such (running) process */
  88. };
  89. if (task_curr(p))
  90. smp_call_function_single(task_cpu(p), remote_function, &data, 1);
  91. return data.ret;
  92. }
  93. /**
  94. * cpu_function_call - call a function on the cpu
  95. * @func: the function to be called
  96. * @info: the function call argument
  97. *
  98. * Calls the function @func on the remote cpu.
  99. *
  100. * returns: @func return value or -ENXIO when the cpu is offline
  101. */
  102. static int cpu_function_call(int cpu, remote_function_f func, void *info)
  103. {
  104. struct remote_function_call data = {
  105. .p = NULL,
  106. .func = func,
  107. .info = info,
  108. .ret = -ENXIO, /* No such CPU */
  109. };
  110. smp_call_function_single(cpu, remote_function, &data, 1);
  111. return data.ret;
  112. }
  113. #define EVENT_OWNER_KERNEL ((void *) -1)
  114. static bool is_kernel_event(struct perf_event *event)
  115. {
  116. return event->owner == EVENT_OWNER_KERNEL;
  117. }
  118. #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
  119. PERF_FLAG_FD_OUTPUT |\
  120. PERF_FLAG_PID_CGROUP |\
  121. PERF_FLAG_FD_CLOEXEC)
  122. /*
  123. * branch priv levels that need permission checks
  124. */
  125. #define PERF_SAMPLE_BRANCH_PERM_PLM \
  126. (PERF_SAMPLE_BRANCH_KERNEL |\
  127. PERF_SAMPLE_BRANCH_HV)
  128. enum event_type_t {
  129. EVENT_FLEXIBLE = 0x1,
  130. EVENT_PINNED = 0x2,
  131. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  132. };
  133. /*
  134. * perf_sched_events : >0 events exist
  135. * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
  136. */
  137. struct static_key_deferred perf_sched_events __read_mostly;
  138. static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
  139. static DEFINE_PER_CPU(int, perf_sched_cb_usages);
  140. static atomic_t nr_mmap_events __read_mostly;
  141. static atomic_t nr_comm_events __read_mostly;
  142. static atomic_t nr_task_events __read_mostly;
  143. static atomic_t nr_freq_events __read_mostly;
  144. static LIST_HEAD(pmus);
  145. static DEFINE_MUTEX(pmus_lock);
  146. static struct srcu_struct pmus_srcu;
  147. /*
  148. * perf event paranoia level:
  149. * -1 - not paranoid at all
  150. * 0 - disallow raw tracepoint access for unpriv
  151. * 1 - disallow cpu events for unpriv
  152. * 2 - disallow kernel profiling for unpriv
  153. */
  154. int sysctl_perf_event_paranoid __read_mostly = 1;
  155. /* Minimum for 512 kiB + 1 user control page */
  156. int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
  157. /*
  158. * max perf event sample rate
  159. */
  160. #define DEFAULT_MAX_SAMPLE_RATE 100000
  161. #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
  162. #define DEFAULT_CPU_TIME_MAX_PERCENT 25
  163. int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
  164. static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
  165. static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
  166. static int perf_sample_allowed_ns __read_mostly =
  167. DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
  168. void update_perf_cpu_limits(void)
  169. {
  170. u64 tmp = perf_sample_period_ns;
  171. tmp *= sysctl_perf_cpu_time_max_percent;
  172. do_div(tmp, 100);
  173. ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
  174. }
  175. static int perf_rotate_context(struct perf_cpu_context *cpuctx);
  176. int perf_proc_update_handler(struct ctl_table *table, int write,
  177. void __user *buffer, size_t *lenp,
  178. loff_t *ppos)
  179. {
  180. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  181. if (ret || !write)
  182. return ret;
  183. max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
  184. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  185. update_perf_cpu_limits();
  186. return 0;
  187. }
  188. int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
  189. int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  190. void __user *buffer, size_t *lenp,
  191. loff_t *ppos)
  192. {
  193. int ret = proc_dointvec(table, write, buffer, lenp, ppos);
  194. if (ret || !write)
  195. return ret;
  196. update_perf_cpu_limits();
  197. return 0;
  198. }
  199. /*
  200. * perf samples are done in some very critical code paths (NMIs).
  201. * If they take too much CPU time, the system can lock up and not
  202. * get any real work done. This will drop the sample rate when
  203. * we detect that events are taking too long.
  204. */
  205. #define NR_ACCUMULATED_SAMPLES 128
  206. static DEFINE_PER_CPU(u64, running_sample_length);
  207. static void perf_duration_warn(struct irq_work *w)
  208. {
  209. u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
  210. u64 avg_local_sample_len;
  211. u64 local_samples_len;
  212. local_samples_len = __this_cpu_read(running_sample_length);
  213. avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
  214. printk_ratelimited(KERN_WARNING
  215. "perf interrupt took too long (%lld > %lld), lowering "
  216. "kernel.perf_event_max_sample_rate to %d\n",
  217. avg_local_sample_len, allowed_ns >> 1,
  218. sysctl_perf_event_sample_rate);
  219. }
  220. static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
  221. void perf_sample_event_took(u64 sample_len_ns)
  222. {
  223. u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
  224. u64 avg_local_sample_len;
  225. u64 local_samples_len;
  226. if (allowed_ns == 0)
  227. return;
  228. /* decay the counter by 1 average sample */
  229. local_samples_len = __this_cpu_read(running_sample_length);
  230. local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
  231. local_samples_len += sample_len_ns;
  232. __this_cpu_write(running_sample_length, local_samples_len);
  233. /*
  234. * note: this will be biased artifically low until we have
  235. * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
  236. * from having to maintain a count.
  237. */
  238. avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
  239. if (avg_local_sample_len <= allowed_ns)
  240. return;
  241. if (max_samples_per_tick <= 1)
  242. return;
  243. max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
  244. sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
  245. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  246. update_perf_cpu_limits();
  247. if (!irq_work_queue(&perf_duration_work)) {
  248. early_printk("perf interrupt took too long (%lld > %lld), lowering "
  249. "kernel.perf_event_max_sample_rate to %d\n",
  250. avg_local_sample_len, allowed_ns >> 1,
  251. sysctl_perf_event_sample_rate);
  252. }
  253. }
  254. static atomic64_t perf_event_id;
  255. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  256. enum event_type_t event_type);
  257. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  258. enum event_type_t event_type,
  259. struct task_struct *task);
  260. static void update_context_time(struct perf_event_context *ctx);
  261. static u64 perf_event_time(struct perf_event *event);
  262. void __weak perf_event_print_debug(void) { }
  263. extern __weak const char *perf_pmu_name(void)
  264. {
  265. return "pmu";
  266. }
  267. static inline u64 perf_clock(void)
  268. {
  269. return local_clock();
  270. }
  271. static inline u64 perf_event_clock(struct perf_event *event)
  272. {
  273. return event->clock();
  274. }
  275. static inline struct perf_cpu_context *
  276. __get_cpu_context(struct perf_event_context *ctx)
  277. {
  278. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  279. }
  280. static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
  281. struct perf_event_context *ctx)
  282. {
  283. raw_spin_lock(&cpuctx->ctx.lock);
  284. if (ctx)
  285. raw_spin_lock(&ctx->lock);
  286. }
  287. static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
  288. struct perf_event_context *ctx)
  289. {
  290. if (ctx)
  291. raw_spin_unlock(&ctx->lock);
  292. raw_spin_unlock(&cpuctx->ctx.lock);
  293. }
  294. #ifdef CONFIG_CGROUP_PERF
  295. static inline bool
  296. perf_cgroup_match(struct perf_event *event)
  297. {
  298. struct perf_event_context *ctx = event->ctx;
  299. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  300. /* @event doesn't care about cgroup */
  301. if (!event->cgrp)
  302. return true;
  303. /* wants specific cgroup scope but @cpuctx isn't associated with any */
  304. if (!cpuctx->cgrp)
  305. return false;
  306. /*
  307. * Cgroup scoping is recursive. An event enabled for a cgroup is
  308. * also enabled for all its descendant cgroups. If @cpuctx's
  309. * cgroup is a descendant of @event's (the test covers identity
  310. * case), it's a match.
  311. */
  312. return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
  313. event->cgrp->css.cgroup);
  314. }
  315. static inline void perf_detach_cgroup(struct perf_event *event)
  316. {
  317. css_put(&event->cgrp->css);
  318. event->cgrp = NULL;
  319. }
  320. static inline int is_cgroup_event(struct perf_event *event)
  321. {
  322. return event->cgrp != NULL;
  323. }
  324. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  325. {
  326. struct perf_cgroup_info *t;
  327. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  328. return t->time;
  329. }
  330. static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
  331. {
  332. struct perf_cgroup_info *info;
  333. u64 now;
  334. now = perf_clock();
  335. info = this_cpu_ptr(cgrp->info);
  336. info->time += now - info->timestamp;
  337. info->timestamp = now;
  338. }
  339. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  340. {
  341. struct perf_cgroup *cgrp_out = cpuctx->cgrp;
  342. if (cgrp_out)
  343. __update_cgrp_time(cgrp_out);
  344. }
  345. static inline void update_cgrp_time_from_event(struct perf_event *event)
  346. {
  347. struct perf_cgroup *cgrp;
  348. /*
  349. * ensure we access cgroup data only when needed and
  350. * when we know the cgroup is pinned (css_get)
  351. */
  352. if (!is_cgroup_event(event))
  353. return;
  354. cgrp = perf_cgroup_from_task(current);
  355. /*
  356. * Do not update time when cgroup is not active
  357. */
  358. if (cgrp == event->cgrp)
  359. __update_cgrp_time(event->cgrp);
  360. }
  361. static inline void
  362. perf_cgroup_set_timestamp(struct task_struct *task,
  363. struct perf_event_context *ctx)
  364. {
  365. struct perf_cgroup *cgrp;
  366. struct perf_cgroup_info *info;
  367. /*
  368. * ctx->lock held by caller
  369. * ensure we do not access cgroup data
  370. * unless we have the cgroup pinned (css_get)
  371. */
  372. if (!task || !ctx->nr_cgroups)
  373. return;
  374. cgrp = perf_cgroup_from_task(task);
  375. info = this_cpu_ptr(cgrp->info);
  376. info->timestamp = ctx->timestamp;
  377. }
  378. #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
  379. #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
  380. /*
  381. * reschedule events based on the cgroup constraint of task.
  382. *
  383. * mode SWOUT : schedule out everything
  384. * mode SWIN : schedule in based on cgroup for next
  385. */
  386. void perf_cgroup_switch(struct task_struct *task, int mode)
  387. {
  388. struct perf_cpu_context *cpuctx;
  389. struct pmu *pmu;
  390. unsigned long flags;
  391. /*
  392. * disable interrupts to avoid geting nr_cgroup
  393. * changes via __perf_event_disable(). Also
  394. * avoids preemption.
  395. */
  396. local_irq_save(flags);
  397. /*
  398. * we reschedule only in the presence of cgroup
  399. * constrained events.
  400. */
  401. rcu_read_lock();
  402. list_for_each_entry_rcu(pmu, &pmus, entry) {
  403. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  404. if (cpuctx->unique_pmu != pmu)
  405. continue; /* ensure we process each cpuctx once */
  406. /*
  407. * perf_cgroup_events says at least one
  408. * context on this CPU has cgroup events.
  409. *
  410. * ctx->nr_cgroups reports the number of cgroup
  411. * events for a context.
  412. */
  413. if (cpuctx->ctx.nr_cgroups > 0) {
  414. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  415. perf_pmu_disable(cpuctx->ctx.pmu);
  416. if (mode & PERF_CGROUP_SWOUT) {
  417. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  418. /*
  419. * must not be done before ctxswout due
  420. * to event_filter_match() in event_sched_out()
  421. */
  422. cpuctx->cgrp = NULL;
  423. }
  424. if (mode & PERF_CGROUP_SWIN) {
  425. WARN_ON_ONCE(cpuctx->cgrp);
  426. /*
  427. * set cgrp before ctxsw in to allow
  428. * event_filter_match() to not have to pass
  429. * task around
  430. */
  431. cpuctx->cgrp = perf_cgroup_from_task(task);
  432. cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
  433. }
  434. perf_pmu_enable(cpuctx->ctx.pmu);
  435. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  436. }
  437. }
  438. rcu_read_unlock();
  439. local_irq_restore(flags);
  440. }
  441. static inline void perf_cgroup_sched_out(struct task_struct *task,
  442. struct task_struct *next)
  443. {
  444. struct perf_cgroup *cgrp1;
  445. struct perf_cgroup *cgrp2 = NULL;
  446. /*
  447. * we come here when we know perf_cgroup_events > 0
  448. */
  449. cgrp1 = perf_cgroup_from_task(task);
  450. /*
  451. * next is NULL when called from perf_event_enable_on_exec()
  452. * that will systematically cause a cgroup_switch()
  453. */
  454. if (next)
  455. cgrp2 = perf_cgroup_from_task(next);
  456. /*
  457. * only schedule out current cgroup events if we know
  458. * that we are switching to a different cgroup. Otherwise,
  459. * do no touch the cgroup events.
  460. */
  461. if (cgrp1 != cgrp2)
  462. perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
  463. }
  464. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  465. struct task_struct *task)
  466. {
  467. struct perf_cgroup *cgrp1;
  468. struct perf_cgroup *cgrp2 = NULL;
  469. /*
  470. * we come here when we know perf_cgroup_events > 0
  471. */
  472. cgrp1 = perf_cgroup_from_task(task);
  473. /* prev can never be NULL */
  474. cgrp2 = perf_cgroup_from_task(prev);
  475. /*
  476. * only need to schedule in cgroup events if we are changing
  477. * cgroup during ctxsw. Cgroup events were not scheduled
  478. * out of ctxsw out if that was not the case.
  479. */
  480. if (cgrp1 != cgrp2)
  481. perf_cgroup_switch(task, PERF_CGROUP_SWIN);
  482. }
  483. static inline int perf_cgroup_connect(int fd, struct perf_event *event,
  484. struct perf_event_attr *attr,
  485. struct perf_event *group_leader)
  486. {
  487. struct perf_cgroup *cgrp;
  488. struct cgroup_subsys_state *css;
  489. struct fd f = fdget(fd);
  490. int ret = 0;
  491. if (!f.file)
  492. return -EBADF;
  493. css = css_tryget_online_from_dir(f.file->f_path.dentry,
  494. &perf_event_cgrp_subsys);
  495. if (IS_ERR(css)) {
  496. ret = PTR_ERR(css);
  497. goto out;
  498. }
  499. cgrp = container_of(css, struct perf_cgroup, css);
  500. event->cgrp = cgrp;
  501. /*
  502. * all events in a group must monitor
  503. * the same cgroup because a task belongs
  504. * to only one perf cgroup at a time
  505. */
  506. if (group_leader && group_leader->cgrp != cgrp) {
  507. perf_detach_cgroup(event);
  508. ret = -EINVAL;
  509. }
  510. out:
  511. fdput(f);
  512. return ret;
  513. }
  514. static inline void
  515. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  516. {
  517. struct perf_cgroup_info *t;
  518. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  519. event->shadow_ctx_time = now - t->timestamp;
  520. }
  521. static inline void
  522. perf_cgroup_defer_enabled(struct perf_event *event)
  523. {
  524. /*
  525. * when the current task's perf cgroup does not match
  526. * the event's, we need to remember to call the
  527. * perf_mark_enable() function the first time a task with
  528. * a matching perf cgroup is scheduled in.
  529. */
  530. if (is_cgroup_event(event) && !perf_cgroup_match(event))
  531. event->cgrp_defer_enabled = 1;
  532. }
  533. static inline void
  534. perf_cgroup_mark_enabled(struct perf_event *event,
  535. struct perf_event_context *ctx)
  536. {
  537. struct perf_event *sub;
  538. u64 tstamp = perf_event_time(event);
  539. if (!event->cgrp_defer_enabled)
  540. return;
  541. event->cgrp_defer_enabled = 0;
  542. event->tstamp_enabled = tstamp - event->total_time_enabled;
  543. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  544. if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
  545. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  546. sub->cgrp_defer_enabled = 0;
  547. }
  548. }
  549. }
  550. #else /* !CONFIG_CGROUP_PERF */
  551. static inline bool
  552. perf_cgroup_match(struct perf_event *event)
  553. {
  554. return true;
  555. }
  556. static inline void perf_detach_cgroup(struct perf_event *event)
  557. {}
  558. static inline int is_cgroup_event(struct perf_event *event)
  559. {
  560. return 0;
  561. }
  562. static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
  563. {
  564. return 0;
  565. }
  566. static inline void update_cgrp_time_from_event(struct perf_event *event)
  567. {
  568. }
  569. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  570. {
  571. }
  572. static inline void perf_cgroup_sched_out(struct task_struct *task,
  573. struct task_struct *next)
  574. {
  575. }
  576. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  577. struct task_struct *task)
  578. {
  579. }
  580. static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
  581. struct perf_event_attr *attr,
  582. struct perf_event *group_leader)
  583. {
  584. return -EINVAL;
  585. }
  586. static inline void
  587. perf_cgroup_set_timestamp(struct task_struct *task,
  588. struct perf_event_context *ctx)
  589. {
  590. }
  591. void
  592. perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
  593. {
  594. }
  595. static inline void
  596. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  597. {
  598. }
  599. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  600. {
  601. return 0;
  602. }
  603. static inline void
  604. perf_cgroup_defer_enabled(struct perf_event *event)
  605. {
  606. }
  607. static inline void
  608. perf_cgroup_mark_enabled(struct perf_event *event,
  609. struct perf_event_context *ctx)
  610. {
  611. }
  612. #endif
  613. /*
  614. * set default to be dependent on timer tick just
  615. * like original code
  616. */
  617. #define PERF_CPU_HRTIMER (1000 / HZ)
  618. /*
  619. * function must be called with interrupts disbled
  620. */
  621. static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
  622. {
  623. struct perf_cpu_context *cpuctx;
  624. int rotations = 0;
  625. WARN_ON(!irqs_disabled());
  626. cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
  627. rotations = perf_rotate_context(cpuctx);
  628. raw_spin_lock(&cpuctx->hrtimer_lock);
  629. if (rotations)
  630. hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
  631. else
  632. cpuctx->hrtimer_active = 0;
  633. raw_spin_unlock(&cpuctx->hrtimer_lock);
  634. return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
  635. }
  636. static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  637. {
  638. struct hrtimer *timer = &cpuctx->hrtimer;
  639. struct pmu *pmu = cpuctx->ctx.pmu;
  640. u64 interval;
  641. /* no multiplexing needed for SW PMU */
  642. if (pmu->task_ctx_nr == perf_sw_context)
  643. return;
  644. /*
  645. * check default is sane, if not set then force to
  646. * default interval (1/tick)
  647. */
  648. interval = pmu->hrtimer_interval_ms;
  649. if (interval < 1)
  650. interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
  651. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
  652. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  653. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
  654. timer->function = perf_mux_hrtimer_handler;
  655. }
  656. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  657. {
  658. struct hrtimer *timer = &cpuctx->hrtimer;
  659. struct pmu *pmu = cpuctx->ctx.pmu;
  660. unsigned long flags;
  661. /* not for SW PMU */
  662. if (pmu->task_ctx_nr == perf_sw_context)
  663. return 0;
  664. raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
  665. if (!cpuctx->hrtimer_active) {
  666. cpuctx->hrtimer_active = 1;
  667. hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
  668. hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
  669. }
  670. raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
  671. return 0;
  672. }
  673. void perf_pmu_disable(struct pmu *pmu)
  674. {
  675. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  676. if (!(*count)++)
  677. pmu->pmu_disable(pmu);
  678. }
  679. void perf_pmu_enable(struct pmu *pmu)
  680. {
  681. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  682. if (!--(*count))
  683. pmu->pmu_enable(pmu);
  684. }
  685. static DEFINE_PER_CPU(struct list_head, active_ctx_list);
  686. /*
  687. * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
  688. * perf_event_task_tick() are fully serialized because they're strictly cpu
  689. * affine and perf_event_ctx{activate,deactivate} are called with IRQs
  690. * disabled, while perf_event_task_tick is called from IRQ context.
  691. */
  692. static void perf_event_ctx_activate(struct perf_event_context *ctx)
  693. {
  694. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  695. WARN_ON(!irqs_disabled());
  696. WARN_ON(!list_empty(&ctx->active_ctx_list));
  697. list_add(&ctx->active_ctx_list, head);
  698. }
  699. static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
  700. {
  701. WARN_ON(!irqs_disabled());
  702. WARN_ON(list_empty(&ctx->active_ctx_list));
  703. list_del_init(&ctx->active_ctx_list);
  704. }
  705. static void get_ctx(struct perf_event_context *ctx)
  706. {
  707. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  708. }
  709. static void free_ctx(struct rcu_head *head)
  710. {
  711. struct perf_event_context *ctx;
  712. ctx = container_of(head, struct perf_event_context, rcu_head);
  713. kfree(ctx->task_ctx_data);
  714. kfree(ctx);
  715. }
  716. static void put_ctx(struct perf_event_context *ctx)
  717. {
  718. if (atomic_dec_and_test(&ctx->refcount)) {
  719. if (ctx->parent_ctx)
  720. put_ctx(ctx->parent_ctx);
  721. if (ctx->task)
  722. put_task_struct(ctx->task);
  723. call_rcu(&ctx->rcu_head, free_ctx);
  724. }
  725. }
  726. /*
  727. * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
  728. * perf_pmu_migrate_context() we need some magic.
  729. *
  730. * Those places that change perf_event::ctx will hold both
  731. * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
  732. *
  733. * Lock ordering is by mutex address. There are two other sites where
  734. * perf_event_context::mutex nests and those are:
  735. *
  736. * - perf_event_exit_task_context() [ child , 0 ]
  737. * __perf_event_exit_task()
  738. * sync_child_event()
  739. * put_event() [ parent, 1 ]
  740. *
  741. * - perf_event_init_context() [ parent, 0 ]
  742. * inherit_task_group()
  743. * inherit_group()
  744. * inherit_event()
  745. * perf_event_alloc()
  746. * perf_init_event()
  747. * perf_try_init_event() [ child , 1 ]
  748. *
  749. * While it appears there is an obvious deadlock here -- the parent and child
  750. * nesting levels are inverted between the two. This is in fact safe because
  751. * life-time rules separate them. That is an exiting task cannot fork, and a
  752. * spawning task cannot (yet) exit.
  753. *
  754. * But remember that that these are parent<->child context relations, and
  755. * migration does not affect children, therefore these two orderings should not
  756. * interact.
  757. *
  758. * The change in perf_event::ctx does not affect children (as claimed above)
  759. * because the sys_perf_event_open() case will install a new event and break
  760. * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
  761. * concerned with cpuctx and that doesn't have children.
  762. *
  763. * The places that change perf_event::ctx will issue:
  764. *
  765. * perf_remove_from_context();
  766. * synchronize_rcu();
  767. * perf_install_in_context();
  768. *
  769. * to affect the change. The remove_from_context() + synchronize_rcu() should
  770. * quiesce the event, after which we can install it in the new location. This
  771. * means that only external vectors (perf_fops, prctl) can perturb the event
  772. * while in transit. Therefore all such accessors should also acquire
  773. * perf_event_context::mutex to serialize against this.
  774. *
  775. * However; because event->ctx can change while we're waiting to acquire
  776. * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
  777. * function.
  778. *
  779. * Lock order:
  780. * task_struct::perf_event_mutex
  781. * perf_event_context::mutex
  782. * perf_event_context::lock
  783. * perf_event::child_mutex;
  784. * perf_event::mmap_mutex
  785. * mmap_sem
  786. */
  787. static struct perf_event_context *
  788. perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
  789. {
  790. struct perf_event_context *ctx;
  791. again:
  792. rcu_read_lock();
  793. ctx = ACCESS_ONCE(event->ctx);
  794. if (!atomic_inc_not_zero(&ctx->refcount)) {
  795. rcu_read_unlock();
  796. goto again;
  797. }
  798. rcu_read_unlock();
  799. mutex_lock_nested(&ctx->mutex, nesting);
  800. if (event->ctx != ctx) {
  801. mutex_unlock(&ctx->mutex);
  802. put_ctx(ctx);
  803. goto again;
  804. }
  805. return ctx;
  806. }
  807. static inline struct perf_event_context *
  808. perf_event_ctx_lock(struct perf_event *event)
  809. {
  810. return perf_event_ctx_lock_nested(event, 0);
  811. }
  812. static void perf_event_ctx_unlock(struct perf_event *event,
  813. struct perf_event_context *ctx)
  814. {
  815. mutex_unlock(&ctx->mutex);
  816. put_ctx(ctx);
  817. }
  818. /*
  819. * This must be done under the ctx->lock, such as to serialize against
  820. * context_equiv(), therefore we cannot call put_ctx() since that might end up
  821. * calling scheduler related locks and ctx->lock nests inside those.
  822. */
  823. static __must_check struct perf_event_context *
  824. unclone_ctx(struct perf_event_context *ctx)
  825. {
  826. struct perf_event_context *parent_ctx = ctx->parent_ctx;
  827. lockdep_assert_held(&ctx->lock);
  828. if (parent_ctx)
  829. ctx->parent_ctx = NULL;
  830. ctx->generation++;
  831. return parent_ctx;
  832. }
  833. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  834. {
  835. /*
  836. * only top level events have the pid namespace they were created in
  837. */
  838. if (event->parent)
  839. event = event->parent;
  840. return task_tgid_nr_ns(p, event->ns);
  841. }
  842. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  843. {
  844. /*
  845. * only top level events have the pid namespace they were created in
  846. */
  847. if (event->parent)
  848. event = event->parent;
  849. return task_pid_nr_ns(p, event->ns);
  850. }
  851. /*
  852. * If we inherit events we want to return the parent event id
  853. * to userspace.
  854. */
  855. static u64 primary_event_id(struct perf_event *event)
  856. {
  857. u64 id = event->id;
  858. if (event->parent)
  859. id = event->parent->id;
  860. return id;
  861. }
  862. /*
  863. * Get the perf_event_context for a task and lock it.
  864. * This has to cope with with the fact that until it is locked,
  865. * the context could get moved to another task.
  866. */
  867. static struct perf_event_context *
  868. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  869. {
  870. struct perf_event_context *ctx;
  871. retry:
  872. /*
  873. * One of the few rules of preemptible RCU is that one cannot do
  874. * rcu_read_unlock() while holding a scheduler (or nested) lock when
  875. * part of the read side critical section was preemptible -- see
  876. * rcu_read_unlock_special().
  877. *
  878. * Since ctx->lock nests under rq->lock we must ensure the entire read
  879. * side critical section is non-preemptible.
  880. */
  881. preempt_disable();
  882. rcu_read_lock();
  883. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  884. if (ctx) {
  885. /*
  886. * If this context is a clone of another, it might
  887. * get swapped for another underneath us by
  888. * perf_event_task_sched_out, though the
  889. * rcu_read_lock() protects us from any context
  890. * getting freed. Lock the context and check if it
  891. * got swapped before we could get the lock, and retry
  892. * if so. If we locked the right context, then it
  893. * can't get swapped on us any more.
  894. */
  895. raw_spin_lock_irqsave(&ctx->lock, *flags);
  896. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  897. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  898. rcu_read_unlock();
  899. preempt_enable();
  900. goto retry;
  901. }
  902. if (!atomic_inc_not_zero(&ctx->refcount)) {
  903. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  904. ctx = NULL;
  905. }
  906. }
  907. rcu_read_unlock();
  908. preempt_enable();
  909. return ctx;
  910. }
  911. /*
  912. * Get the context for a task and increment its pin_count so it
  913. * can't get swapped to another task. This also increments its
  914. * reference count so that the context can't get freed.
  915. */
  916. static struct perf_event_context *
  917. perf_pin_task_context(struct task_struct *task, int ctxn)
  918. {
  919. struct perf_event_context *ctx;
  920. unsigned long flags;
  921. ctx = perf_lock_task_context(task, ctxn, &flags);
  922. if (ctx) {
  923. ++ctx->pin_count;
  924. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  925. }
  926. return ctx;
  927. }
  928. static void perf_unpin_context(struct perf_event_context *ctx)
  929. {
  930. unsigned long flags;
  931. raw_spin_lock_irqsave(&ctx->lock, flags);
  932. --ctx->pin_count;
  933. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  934. }
  935. /*
  936. * Update the record of the current time in a context.
  937. */
  938. static void update_context_time(struct perf_event_context *ctx)
  939. {
  940. u64 now = perf_clock();
  941. ctx->time += now - ctx->timestamp;
  942. ctx->timestamp = now;
  943. }
  944. static u64 perf_event_time(struct perf_event *event)
  945. {
  946. struct perf_event_context *ctx = event->ctx;
  947. if (is_cgroup_event(event))
  948. return perf_cgroup_event_time(event);
  949. return ctx ? ctx->time : 0;
  950. }
  951. /*
  952. * Update the total_time_enabled and total_time_running fields for a event.
  953. * The caller of this function needs to hold the ctx->lock.
  954. */
  955. static void update_event_times(struct perf_event *event)
  956. {
  957. struct perf_event_context *ctx = event->ctx;
  958. u64 run_end;
  959. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  960. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  961. return;
  962. /*
  963. * in cgroup mode, time_enabled represents
  964. * the time the event was enabled AND active
  965. * tasks were in the monitored cgroup. This is
  966. * independent of the activity of the context as
  967. * there may be a mix of cgroup and non-cgroup events.
  968. *
  969. * That is why we treat cgroup events differently
  970. * here.
  971. */
  972. if (is_cgroup_event(event))
  973. run_end = perf_cgroup_event_time(event);
  974. else if (ctx->is_active)
  975. run_end = ctx->time;
  976. else
  977. run_end = event->tstamp_stopped;
  978. event->total_time_enabled = run_end - event->tstamp_enabled;
  979. if (event->state == PERF_EVENT_STATE_INACTIVE)
  980. run_end = event->tstamp_stopped;
  981. else
  982. run_end = perf_event_time(event);
  983. event->total_time_running = run_end - event->tstamp_running;
  984. }
  985. /*
  986. * Update total_time_enabled and total_time_running for all events in a group.
  987. */
  988. static void update_group_times(struct perf_event *leader)
  989. {
  990. struct perf_event *event;
  991. update_event_times(leader);
  992. list_for_each_entry(event, &leader->sibling_list, group_entry)
  993. update_event_times(event);
  994. }
  995. static struct list_head *
  996. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  997. {
  998. if (event->attr.pinned)
  999. return &ctx->pinned_groups;
  1000. else
  1001. return &ctx->flexible_groups;
  1002. }
  1003. /*
  1004. * Add a event from the lists for its context.
  1005. * Must be called with ctx->mutex and ctx->lock held.
  1006. */
  1007. static void
  1008. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  1009. {
  1010. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  1011. event->attach_state |= PERF_ATTACH_CONTEXT;
  1012. /*
  1013. * If we're a stand alone event or group leader, we go to the context
  1014. * list, group events are kept attached to the group so that
  1015. * perf_group_detach can, at all times, locate all siblings.
  1016. */
  1017. if (event->group_leader == event) {
  1018. struct list_head *list;
  1019. if (is_software_event(event))
  1020. event->group_flags |= PERF_GROUP_SOFTWARE;
  1021. list = ctx_group_list(event, ctx);
  1022. list_add_tail(&event->group_entry, list);
  1023. }
  1024. if (is_cgroup_event(event))
  1025. ctx->nr_cgroups++;
  1026. list_add_rcu(&event->event_entry, &ctx->event_list);
  1027. ctx->nr_events++;
  1028. if (event->attr.inherit_stat)
  1029. ctx->nr_stat++;
  1030. ctx->generation++;
  1031. }
  1032. /*
  1033. * Initialize event state based on the perf_event_attr::disabled.
  1034. */
  1035. static inline void perf_event__state_init(struct perf_event *event)
  1036. {
  1037. event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
  1038. PERF_EVENT_STATE_INACTIVE;
  1039. }
  1040. /*
  1041. * Called at perf_event creation and when events are attached/detached from a
  1042. * group.
  1043. */
  1044. static void perf_event__read_size(struct perf_event *event)
  1045. {
  1046. int entry = sizeof(u64); /* value */
  1047. int size = 0;
  1048. int nr = 1;
  1049. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1050. size += sizeof(u64);
  1051. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1052. size += sizeof(u64);
  1053. if (event->attr.read_format & PERF_FORMAT_ID)
  1054. entry += sizeof(u64);
  1055. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1056. nr += event->group_leader->nr_siblings;
  1057. size += sizeof(u64);
  1058. }
  1059. size += entry * nr;
  1060. event->read_size = size;
  1061. }
  1062. static void perf_event__header_size(struct perf_event *event)
  1063. {
  1064. struct perf_sample_data *data;
  1065. u64 sample_type = event->attr.sample_type;
  1066. u16 size = 0;
  1067. perf_event__read_size(event);
  1068. if (sample_type & PERF_SAMPLE_IP)
  1069. size += sizeof(data->ip);
  1070. if (sample_type & PERF_SAMPLE_ADDR)
  1071. size += sizeof(data->addr);
  1072. if (sample_type & PERF_SAMPLE_PERIOD)
  1073. size += sizeof(data->period);
  1074. if (sample_type & PERF_SAMPLE_WEIGHT)
  1075. size += sizeof(data->weight);
  1076. if (sample_type & PERF_SAMPLE_READ)
  1077. size += event->read_size;
  1078. if (sample_type & PERF_SAMPLE_DATA_SRC)
  1079. size += sizeof(data->data_src.val);
  1080. if (sample_type & PERF_SAMPLE_TRANSACTION)
  1081. size += sizeof(data->txn);
  1082. event->header_size = size;
  1083. }
  1084. static void perf_event__id_header_size(struct perf_event *event)
  1085. {
  1086. struct perf_sample_data *data;
  1087. u64 sample_type = event->attr.sample_type;
  1088. u16 size = 0;
  1089. if (sample_type & PERF_SAMPLE_TID)
  1090. size += sizeof(data->tid_entry);
  1091. if (sample_type & PERF_SAMPLE_TIME)
  1092. size += sizeof(data->time);
  1093. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  1094. size += sizeof(data->id);
  1095. if (sample_type & PERF_SAMPLE_ID)
  1096. size += sizeof(data->id);
  1097. if (sample_type & PERF_SAMPLE_STREAM_ID)
  1098. size += sizeof(data->stream_id);
  1099. if (sample_type & PERF_SAMPLE_CPU)
  1100. size += sizeof(data->cpu_entry);
  1101. event->id_header_size = size;
  1102. }
  1103. static void perf_group_attach(struct perf_event *event)
  1104. {
  1105. struct perf_event *group_leader = event->group_leader, *pos;
  1106. /*
  1107. * We can have double attach due to group movement in perf_event_open.
  1108. */
  1109. if (event->attach_state & PERF_ATTACH_GROUP)
  1110. return;
  1111. event->attach_state |= PERF_ATTACH_GROUP;
  1112. if (group_leader == event)
  1113. return;
  1114. WARN_ON_ONCE(group_leader->ctx != event->ctx);
  1115. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  1116. !is_software_event(event))
  1117. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  1118. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  1119. group_leader->nr_siblings++;
  1120. perf_event__header_size(group_leader);
  1121. list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
  1122. perf_event__header_size(pos);
  1123. }
  1124. /*
  1125. * Remove a event from the lists for its context.
  1126. * Must be called with ctx->mutex and ctx->lock held.
  1127. */
  1128. static void
  1129. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  1130. {
  1131. struct perf_cpu_context *cpuctx;
  1132. WARN_ON_ONCE(event->ctx != ctx);
  1133. lockdep_assert_held(&ctx->lock);
  1134. /*
  1135. * We can have double detach due to exit/hot-unplug + close.
  1136. */
  1137. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  1138. return;
  1139. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  1140. if (is_cgroup_event(event)) {
  1141. ctx->nr_cgroups--;
  1142. cpuctx = __get_cpu_context(ctx);
  1143. /*
  1144. * if there are no more cgroup events
  1145. * then cler cgrp to avoid stale pointer
  1146. * in update_cgrp_time_from_cpuctx()
  1147. */
  1148. if (!ctx->nr_cgroups)
  1149. cpuctx->cgrp = NULL;
  1150. }
  1151. ctx->nr_events--;
  1152. if (event->attr.inherit_stat)
  1153. ctx->nr_stat--;
  1154. list_del_rcu(&event->event_entry);
  1155. if (event->group_leader == event)
  1156. list_del_init(&event->group_entry);
  1157. update_group_times(event);
  1158. /*
  1159. * If event was in error state, then keep it
  1160. * that way, otherwise bogus counts will be
  1161. * returned on read(). The only way to get out
  1162. * of error state is by explicit re-enabling
  1163. * of the event
  1164. */
  1165. if (event->state > PERF_EVENT_STATE_OFF)
  1166. event->state = PERF_EVENT_STATE_OFF;
  1167. ctx->generation++;
  1168. }
  1169. static void perf_group_detach(struct perf_event *event)
  1170. {
  1171. struct perf_event *sibling, *tmp;
  1172. struct list_head *list = NULL;
  1173. /*
  1174. * We can have double detach due to exit/hot-unplug + close.
  1175. */
  1176. if (!(event->attach_state & PERF_ATTACH_GROUP))
  1177. return;
  1178. event->attach_state &= ~PERF_ATTACH_GROUP;
  1179. /*
  1180. * If this is a sibling, remove it from its group.
  1181. */
  1182. if (event->group_leader != event) {
  1183. list_del_init(&event->group_entry);
  1184. event->group_leader->nr_siblings--;
  1185. goto out;
  1186. }
  1187. if (!list_empty(&event->group_entry))
  1188. list = &event->group_entry;
  1189. /*
  1190. * If this was a group event with sibling events then
  1191. * upgrade the siblings to singleton events by adding them
  1192. * to whatever list we are on.
  1193. */
  1194. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  1195. if (list)
  1196. list_move_tail(&sibling->group_entry, list);
  1197. sibling->group_leader = sibling;
  1198. /* Inherit group flags from the previous leader */
  1199. sibling->group_flags = event->group_flags;
  1200. WARN_ON_ONCE(sibling->ctx != event->ctx);
  1201. }
  1202. out:
  1203. perf_event__header_size(event->group_leader);
  1204. list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
  1205. perf_event__header_size(tmp);
  1206. }
  1207. /*
  1208. * User event without the task.
  1209. */
  1210. static bool is_orphaned_event(struct perf_event *event)
  1211. {
  1212. return event && !is_kernel_event(event) && !event->owner;
  1213. }
  1214. /*
  1215. * Event has a parent but parent's task finished and it's
  1216. * alive only because of children holding refference.
  1217. */
  1218. static bool is_orphaned_child(struct perf_event *event)
  1219. {
  1220. return is_orphaned_event(event->parent);
  1221. }
  1222. static void orphans_remove_work(struct work_struct *work);
  1223. static void schedule_orphans_remove(struct perf_event_context *ctx)
  1224. {
  1225. if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
  1226. return;
  1227. if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
  1228. get_ctx(ctx);
  1229. ctx->orphans_remove_sched = true;
  1230. }
  1231. }
  1232. static int __init perf_workqueue_init(void)
  1233. {
  1234. perf_wq = create_singlethread_workqueue("perf");
  1235. WARN(!perf_wq, "failed to create perf workqueue\n");
  1236. return perf_wq ? 0 : -1;
  1237. }
  1238. core_initcall(perf_workqueue_init);
  1239. static inline int pmu_filter_match(struct perf_event *event)
  1240. {
  1241. struct pmu *pmu = event->pmu;
  1242. return pmu->filter_match ? pmu->filter_match(event) : 1;
  1243. }
  1244. static inline int
  1245. event_filter_match(struct perf_event *event)
  1246. {
  1247. return (event->cpu == -1 || event->cpu == smp_processor_id())
  1248. && perf_cgroup_match(event) && pmu_filter_match(event);
  1249. }
  1250. static void
  1251. event_sched_out(struct perf_event *event,
  1252. struct perf_cpu_context *cpuctx,
  1253. struct perf_event_context *ctx)
  1254. {
  1255. u64 tstamp = perf_event_time(event);
  1256. u64 delta;
  1257. WARN_ON_ONCE(event->ctx != ctx);
  1258. lockdep_assert_held(&ctx->lock);
  1259. /*
  1260. * An event which could not be activated because of
  1261. * filter mismatch still needs to have its timings
  1262. * maintained, otherwise bogus information is return
  1263. * via read() for time_enabled, time_running:
  1264. */
  1265. if (event->state == PERF_EVENT_STATE_INACTIVE
  1266. && !event_filter_match(event)) {
  1267. delta = tstamp - event->tstamp_stopped;
  1268. event->tstamp_running += delta;
  1269. event->tstamp_stopped = tstamp;
  1270. }
  1271. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1272. return;
  1273. perf_pmu_disable(event->pmu);
  1274. event->state = PERF_EVENT_STATE_INACTIVE;
  1275. if (event->pending_disable) {
  1276. event->pending_disable = 0;
  1277. event->state = PERF_EVENT_STATE_OFF;
  1278. }
  1279. event->tstamp_stopped = tstamp;
  1280. event->pmu->del(event, 0);
  1281. event->oncpu = -1;
  1282. if (!is_software_event(event))
  1283. cpuctx->active_oncpu--;
  1284. if (!--ctx->nr_active)
  1285. perf_event_ctx_deactivate(ctx);
  1286. if (event->attr.freq && event->attr.sample_freq)
  1287. ctx->nr_freq--;
  1288. if (event->attr.exclusive || !cpuctx->active_oncpu)
  1289. cpuctx->exclusive = 0;
  1290. if (is_orphaned_child(event))
  1291. schedule_orphans_remove(ctx);
  1292. perf_pmu_enable(event->pmu);
  1293. }
  1294. static void
  1295. group_sched_out(struct perf_event *group_event,
  1296. struct perf_cpu_context *cpuctx,
  1297. struct perf_event_context *ctx)
  1298. {
  1299. struct perf_event *event;
  1300. int state = group_event->state;
  1301. event_sched_out(group_event, cpuctx, ctx);
  1302. /*
  1303. * Schedule out siblings (if any):
  1304. */
  1305. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  1306. event_sched_out(event, cpuctx, ctx);
  1307. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  1308. cpuctx->exclusive = 0;
  1309. }
  1310. struct remove_event {
  1311. struct perf_event *event;
  1312. bool detach_group;
  1313. };
  1314. /*
  1315. * Cross CPU call to remove a performance event
  1316. *
  1317. * We disable the event on the hardware level first. After that we
  1318. * remove it from the context list.
  1319. */
  1320. static int __perf_remove_from_context(void *info)
  1321. {
  1322. struct remove_event *re = info;
  1323. struct perf_event *event = re->event;
  1324. struct perf_event_context *ctx = event->ctx;
  1325. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1326. raw_spin_lock(&ctx->lock);
  1327. event_sched_out(event, cpuctx, ctx);
  1328. if (re->detach_group)
  1329. perf_group_detach(event);
  1330. list_del_event(event, ctx);
  1331. if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
  1332. ctx->is_active = 0;
  1333. cpuctx->task_ctx = NULL;
  1334. }
  1335. raw_spin_unlock(&ctx->lock);
  1336. return 0;
  1337. }
  1338. /*
  1339. * Remove the event from a task's (or a CPU's) list of events.
  1340. *
  1341. * CPU events are removed with a smp call. For task events we only
  1342. * call when the task is on a CPU.
  1343. *
  1344. * If event->ctx is a cloned context, callers must make sure that
  1345. * every task struct that event->ctx->task could possibly point to
  1346. * remains valid. This is OK when called from perf_release since
  1347. * that only calls us on the top-level context, which can't be a clone.
  1348. * When called from perf_event_exit_task, it's OK because the
  1349. * context has been detached from its task.
  1350. */
  1351. static void perf_remove_from_context(struct perf_event *event, bool detach_group)
  1352. {
  1353. struct perf_event_context *ctx = event->ctx;
  1354. struct task_struct *task = ctx->task;
  1355. struct remove_event re = {
  1356. .event = event,
  1357. .detach_group = detach_group,
  1358. };
  1359. lockdep_assert_held(&ctx->mutex);
  1360. if (!task) {
  1361. /*
  1362. * Per cpu events are removed via an smp call. The removal can
  1363. * fail if the CPU is currently offline, but in that case we
  1364. * already called __perf_remove_from_context from
  1365. * perf_event_exit_cpu.
  1366. */
  1367. cpu_function_call(event->cpu, __perf_remove_from_context, &re);
  1368. return;
  1369. }
  1370. retry:
  1371. if (!task_function_call(task, __perf_remove_from_context, &re))
  1372. return;
  1373. raw_spin_lock_irq(&ctx->lock);
  1374. /*
  1375. * If we failed to find a running task, but find the context active now
  1376. * that we've acquired the ctx->lock, retry.
  1377. */
  1378. if (ctx->is_active) {
  1379. raw_spin_unlock_irq(&ctx->lock);
  1380. /*
  1381. * Reload the task pointer, it might have been changed by
  1382. * a concurrent perf_event_context_sched_out().
  1383. */
  1384. task = ctx->task;
  1385. goto retry;
  1386. }
  1387. /*
  1388. * Since the task isn't running, its safe to remove the event, us
  1389. * holding the ctx->lock ensures the task won't get scheduled in.
  1390. */
  1391. if (detach_group)
  1392. perf_group_detach(event);
  1393. list_del_event(event, ctx);
  1394. raw_spin_unlock_irq(&ctx->lock);
  1395. }
  1396. /*
  1397. * Cross CPU call to disable a performance event
  1398. */
  1399. int __perf_event_disable(void *info)
  1400. {
  1401. struct perf_event *event = info;
  1402. struct perf_event_context *ctx = event->ctx;
  1403. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1404. /*
  1405. * If this is a per-task event, need to check whether this
  1406. * event's task is the current task on this cpu.
  1407. *
  1408. * Can trigger due to concurrent perf_event_context_sched_out()
  1409. * flipping contexts around.
  1410. */
  1411. if (ctx->task && cpuctx->task_ctx != ctx)
  1412. return -EINVAL;
  1413. raw_spin_lock(&ctx->lock);
  1414. /*
  1415. * If the event is on, turn it off.
  1416. * If it is in error state, leave it in error state.
  1417. */
  1418. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  1419. update_context_time(ctx);
  1420. update_cgrp_time_from_event(event);
  1421. update_group_times(event);
  1422. if (event == event->group_leader)
  1423. group_sched_out(event, cpuctx, ctx);
  1424. else
  1425. event_sched_out(event, cpuctx, ctx);
  1426. event->state = PERF_EVENT_STATE_OFF;
  1427. }
  1428. raw_spin_unlock(&ctx->lock);
  1429. return 0;
  1430. }
  1431. /*
  1432. * Disable a event.
  1433. *
  1434. * If event->ctx is a cloned context, callers must make sure that
  1435. * every task struct that event->ctx->task could possibly point to
  1436. * remains valid. This condition is satisifed when called through
  1437. * perf_event_for_each_child or perf_event_for_each because they
  1438. * hold the top-level event's child_mutex, so any descendant that
  1439. * goes to exit will block in sync_child_event.
  1440. * When called from perf_pending_event it's OK because event->ctx
  1441. * is the current context on this CPU and preemption is disabled,
  1442. * hence we can't get into perf_event_task_sched_out for this context.
  1443. */
  1444. static void _perf_event_disable(struct perf_event *event)
  1445. {
  1446. struct perf_event_context *ctx = event->ctx;
  1447. struct task_struct *task = ctx->task;
  1448. if (!task) {
  1449. /*
  1450. * Disable the event on the cpu that it's on
  1451. */
  1452. cpu_function_call(event->cpu, __perf_event_disable, event);
  1453. return;
  1454. }
  1455. retry:
  1456. if (!task_function_call(task, __perf_event_disable, event))
  1457. return;
  1458. raw_spin_lock_irq(&ctx->lock);
  1459. /*
  1460. * If the event is still active, we need to retry the cross-call.
  1461. */
  1462. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1463. raw_spin_unlock_irq(&ctx->lock);
  1464. /*
  1465. * Reload the task pointer, it might have been changed by
  1466. * a concurrent perf_event_context_sched_out().
  1467. */
  1468. task = ctx->task;
  1469. goto retry;
  1470. }
  1471. /*
  1472. * Since we have the lock this context can't be scheduled
  1473. * in, so we can change the state safely.
  1474. */
  1475. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1476. update_group_times(event);
  1477. event->state = PERF_EVENT_STATE_OFF;
  1478. }
  1479. raw_spin_unlock_irq(&ctx->lock);
  1480. }
  1481. /*
  1482. * Strictly speaking kernel users cannot create groups and therefore this
  1483. * interface does not need the perf_event_ctx_lock() magic.
  1484. */
  1485. void perf_event_disable(struct perf_event *event)
  1486. {
  1487. struct perf_event_context *ctx;
  1488. ctx = perf_event_ctx_lock(event);
  1489. _perf_event_disable(event);
  1490. perf_event_ctx_unlock(event, ctx);
  1491. }
  1492. EXPORT_SYMBOL_GPL(perf_event_disable);
  1493. static void perf_set_shadow_time(struct perf_event *event,
  1494. struct perf_event_context *ctx,
  1495. u64 tstamp)
  1496. {
  1497. /*
  1498. * use the correct time source for the time snapshot
  1499. *
  1500. * We could get by without this by leveraging the
  1501. * fact that to get to this function, the caller
  1502. * has most likely already called update_context_time()
  1503. * and update_cgrp_time_xx() and thus both timestamp
  1504. * are identical (or very close). Given that tstamp is,
  1505. * already adjusted for cgroup, we could say that:
  1506. * tstamp - ctx->timestamp
  1507. * is equivalent to
  1508. * tstamp - cgrp->timestamp.
  1509. *
  1510. * Then, in perf_output_read(), the calculation would
  1511. * work with no changes because:
  1512. * - event is guaranteed scheduled in
  1513. * - no scheduled out in between
  1514. * - thus the timestamp would be the same
  1515. *
  1516. * But this is a bit hairy.
  1517. *
  1518. * So instead, we have an explicit cgroup call to remain
  1519. * within the time time source all along. We believe it
  1520. * is cleaner and simpler to understand.
  1521. */
  1522. if (is_cgroup_event(event))
  1523. perf_cgroup_set_shadow_time(event, tstamp);
  1524. else
  1525. event->shadow_ctx_time = tstamp - ctx->timestamp;
  1526. }
  1527. #define MAX_INTERRUPTS (~0ULL)
  1528. static void perf_log_throttle(struct perf_event *event, int enable);
  1529. static void perf_log_itrace_start(struct perf_event *event);
  1530. static int
  1531. event_sched_in(struct perf_event *event,
  1532. struct perf_cpu_context *cpuctx,
  1533. struct perf_event_context *ctx)
  1534. {
  1535. u64 tstamp = perf_event_time(event);
  1536. int ret = 0;
  1537. lockdep_assert_held(&ctx->lock);
  1538. if (event->state <= PERF_EVENT_STATE_OFF)
  1539. return 0;
  1540. event->state = PERF_EVENT_STATE_ACTIVE;
  1541. event->oncpu = smp_processor_id();
  1542. /*
  1543. * Unthrottle events, since we scheduled we might have missed several
  1544. * ticks already, also for a heavily scheduling task there is little
  1545. * guarantee it'll get a tick in a timely manner.
  1546. */
  1547. if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
  1548. perf_log_throttle(event, 1);
  1549. event->hw.interrupts = 0;
  1550. }
  1551. /*
  1552. * The new state must be visible before we turn it on in the hardware:
  1553. */
  1554. smp_wmb();
  1555. perf_pmu_disable(event->pmu);
  1556. event->tstamp_running += tstamp - event->tstamp_stopped;
  1557. perf_set_shadow_time(event, ctx, tstamp);
  1558. perf_log_itrace_start(event);
  1559. if (event->pmu->add(event, PERF_EF_START)) {
  1560. event->state = PERF_EVENT_STATE_INACTIVE;
  1561. event->oncpu = -1;
  1562. ret = -EAGAIN;
  1563. goto out;
  1564. }
  1565. if (!is_software_event(event))
  1566. cpuctx->active_oncpu++;
  1567. if (!ctx->nr_active++)
  1568. perf_event_ctx_activate(ctx);
  1569. if (event->attr.freq && event->attr.sample_freq)
  1570. ctx->nr_freq++;
  1571. if (event->attr.exclusive)
  1572. cpuctx->exclusive = 1;
  1573. if (is_orphaned_child(event))
  1574. schedule_orphans_remove(ctx);
  1575. out:
  1576. perf_pmu_enable(event->pmu);
  1577. return ret;
  1578. }
  1579. static int
  1580. group_sched_in(struct perf_event *group_event,
  1581. struct perf_cpu_context *cpuctx,
  1582. struct perf_event_context *ctx)
  1583. {
  1584. struct perf_event *event, *partial_group = NULL;
  1585. struct pmu *pmu = ctx->pmu;
  1586. u64 now = ctx->time;
  1587. bool simulate = false;
  1588. if (group_event->state == PERF_EVENT_STATE_OFF)
  1589. return 0;
  1590. pmu->start_txn(pmu);
  1591. if (event_sched_in(group_event, cpuctx, ctx)) {
  1592. pmu->cancel_txn(pmu);
  1593. perf_mux_hrtimer_restart(cpuctx);
  1594. return -EAGAIN;
  1595. }
  1596. /*
  1597. * Schedule in siblings as one group (if any):
  1598. */
  1599. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1600. if (event_sched_in(event, cpuctx, ctx)) {
  1601. partial_group = event;
  1602. goto group_error;
  1603. }
  1604. }
  1605. if (!pmu->commit_txn(pmu))
  1606. return 0;
  1607. group_error:
  1608. /*
  1609. * Groups can be scheduled in as one unit only, so undo any
  1610. * partial group before returning:
  1611. * The events up to the failed event are scheduled out normally,
  1612. * tstamp_stopped will be updated.
  1613. *
  1614. * The failed events and the remaining siblings need to have
  1615. * their timings updated as if they had gone thru event_sched_in()
  1616. * and event_sched_out(). This is required to get consistent timings
  1617. * across the group. This also takes care of the case where the group
  1618. * could never be scheduled by ensuring tstamp_stopped is set to mark
  1619. * the time the event was actually stopped, such that time delta
  1620. * calculation in update_event_times() is correct.
  1621. */
  1622. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1623. if (event == partial_group)
  1624. simulate = true;
  1625. if (simulate) {
  1626. event->tstamp_running += now - event->tstamp_stopped;
  1627. event->tstamp_stopped = now;
  1628. } else {
  1629. event_sched_out(event, cpuctx, ctx);
  1630. }
  1631. }
  1632. event_sched_out(group_event, cpuctx, ctx);
  1633. pmu->cancel_txn(pmu);
  1634. perf_mux_hrtimer_restart(cpuctx);
  1635. return -EAGAIN;
  1636. }
  1637. /*
  1638. * Work out whether we can put this event group on the CPU now.
  1639. */
  1640. static int group_can_go_on(struct perf_event *event,
  1641. struct perf_cpu_context *cpuctx,
  1642. int can_add_hw)
  1643. {
  1644. /*
  1645. * Groups consisting entirely of software events can always go on.
  1646. */
  1647. if (event->group_flags & PERF_GROUP_SOFTWARE)
  1648. return 1;
  1649. /*
  1650. * If an exclusive group is already on, no other hardware
  1651. * events can go on.
  1652. */
  1653. if (cpuctx->exclusive)
  1654. return 0;
  1655. /*
  1656. * If this group is exclusive and there are already
  1657. * events on the CPU, it can't go on.
  1658. */
  1659. if (event->attr.exclusive && cpuctx->active_oncpu)
  1660. return 0;
  1661. /*
  1662. * Otherwise, try to add it if all previous groups were able
  1663. * to go on.
  1664. */
  1665. return can_add_hw;
  1666. }
  1667. static void add_event_to_ctx(struct perf_event *event,
  1668. struct perf_event_context *ctx)
  1669. {
  1670. u64 tstamp = perf_event_time(event);
  1671. list_add_event(event, ctx);
  1672. perf_group_attach(event);
  1673. event->tstamp_enabled = tstamp;
  1674. event->tstamp_running = tstamp;
  1675. event->tstamp_stopped = tstamp;
  1676. }
  1677. static void task_ctx_sched_out(struct perf_event_context *ctx);
  1678. static void
  1679. ctx_sched_in(struct perf_event_context *ctx,
  1680. struct perf_cpu_context *cpuctx,
  1681. enum event_type_t event_type,
  1682. struct task_struct *task);
  1683. static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
  1684. struct perf_event_context *ctx,
  1685. struct task_struct *task)
  1686. {
  1687. cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
  1688. if (ctx)
  1689. ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
  1690. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
  1691. if (ctx)
  1692. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
  1693. }
  1694. /*
  1695. * Cross CPU call to install and enable a performance event
  1696. *
  1697. * Must be called with ctx->mutex held
  1698. */
  1699. static int __perf_install_in_context(void *info)
  1700. {
  1701. struct perf_event *event = info;
  1702. struct perf_event_context *ctx = event->ctx;
  1703. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1704. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  1705. struct task_struct *task = current;
  1706. perf_ctx_lock(cpuctx, task_ctx);
  1707. perf_pmu_disable(cpuctx->ctx.pmu);
  1708. /*
  1709. * If there was an active task_ctx schedule it out.
  1710. */
  1711. if (task_ctx)
  1712. task_ctx_sched_out(task_ctx);
  1713. /*
  1714. * If the context we're installing events in is not the
  1715. * active task_ctx, flip them.
  1716. */
  1717. if (ctx->task && task_ctx != ctx) {
  1718. if (task_ctx)
  1719. raw_spin_unlock(&task_ctx->lock);
  1720. raw_spin_lock(&ctx->lock);
  1721. task_ctx = ctx;
  1722. }
  1723. if (task_ctx) {
  1724. cpuctx->task_ctx = task_ctx;
  1725. task = task_ctx->task;
  1726. }
  1727. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  1728. update_context_time(ctx);
  1729. /*
  1730. * update cgrp time only if current cgrp
  1731. * matches event->cgrp. Must be done before
  1732. * calling add_event_to_ctx()
  1733. */
  1734. update_cgrp_time_from_event(event);
  1735. add_event_to_ctx(event, ctx);
  1736. /*
  1737. * Schedule everything back in
  1738. */
  1739. perf_event_sched_in(cpuctx, task_ctx, task);
  1740. perf_pmu_enable(cpuctx->ctx.pmu);
  1741. perf_ctx_unlock(cpuctx, task_ctx);
  1742. return 0;
  1743. }
  1744. /*
  1745. * Attach a performance event to a context
  1746. *
  1747. * First we add the event to the list with the hardware enable bit
  1748. * in event->hw_config cleared.
  1749. *
  1750. * If the event is attached to a task which is on a CPU we use a smp
  1751. * call to enable it in the task context. The task might have been
  1752. * scheduled away, but we check this in the smp call again.
  1753. */
  1754. static void
  1755. perf_install_in_context(struct perf_event_context *ctx,
  1756. struct perf_event *event,
  1757. int cpu)
  1758. {
  1759. struct task_struct *task = ctx->task;
  1760. lockdep_assert_held(&ctx->mutex);
  1761. event->ctx = ctx;
  1762. if (event->cpu != -1)
  1763. event->cpu = cpu;
  1764. if (!task) {
  1765. /*
  1766. * Per cpu events are installed via an smp call and
  1767. * the install is always successful.
  1768. */
  1769. cpu_function_call(cpu, __perf_install_in_context, event);
  1770. return;
  1771. }
  1772. retry:
  1773. if (!task_function_call(task, __perf_install_in_context, event))
  1774. return;
  1775. raw_spin_lock_irq(&ctx->lock);
  1776. /*
  1777. * If we failed to find a running task, but find the context active now
  1778. * that we've acquired the ctx->lock, retry.
  1779. */
  1780. if (ctx->is_active) {
  1781. raw_spin_unlock_irq(&ctx->lock);
  1782. /*
  1783. * Reload the task pointer, it might have been changed by
  1784. * a concurrent perf_event_context_sched_out().
  1785. */
  1786. task = ctx->task;
  1787. goto retry;
  1788. }
  1789. /*
  1790. * Since the task isn't running, its safe to add the event, us holding
  1791. * the ctx->lock ensures the task won't get scheduled in.
  1792. */
  1793. add_event_to_ctx(event, ctx);
  1794. raw_spin_unlock_irq(&ctx->lock);
  1795. }
  1796. /*
  1797. * Put a event into inactive state and update time fields.
  1798. * Enabling the leader of a group effectively enables all
  1799. * the group members that aren't explicitly disabled, so we
  1800. * have to update their ->tstamp_enabled also.
  1801. * Note: this works for group members as well as group leaders
  1802. * since the non-leader members' sibling_lists will be empty.
  1803. */
  1804. static void __perf_event_mark_enabled(struct perf_event *event)
  1805. {
  1806. struct perf_event *sub;
  1807. u64 tstamp = perf_event_time(event);
  1808. event->state = PERF_EVENT_STATE_INACTIVE;
  1809. event->tstamp_enabled = tstamp - event->total_time_enabled;
  1810. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  1811. if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  1812. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  1813. }
  1814. }
  1815. /*
  1816. * Cross CPU call to enable a performance event
  1817. */
  1818. static int __perf_event_enable(void *info)
  1819. {
  1820. struct perf_event *event = info;
  1821. struct perf_event_context *ctx = event->ctx;
  1822. struct perf_event *leader = event->group_leader;
  1823. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1824. int err;
  1825. /*
  1826. * There's a time window between 'ctx->is_active' check
  1827. * in perf_event_enable function and this place having:
  1828. * - IRQs on
  1829. * - ctx->lock unlocked
  1830. *
  1831. * where the task could be killed and 'ctx' deactivated
  1832. * by perf_event_exit_task.
  1833. */
  1834. if (!ctx->is_active)
  1835. return -EINVAL;
  1836. raw_spin_lock(&ctx->lock);
  1837. update_context_time(ctx);
  1838. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1839. goto unlock;
  1840. /*
  1841. * set current task's cgroup time reference point
  1842. */
  1843. perf_cgroup_set_timestamp(current, ctx);
  1844. __perf_event_mark_enabled(event);
  1845. if (!event_filter_match(event)) {
  1846. if (is_cgroup_event(event))
  1847. perf_cgroup_defer_enabled(event);
  1848. goto unlock;
  1849. }
  1850. /*
  1851. * If the event is in a group and isn't the group leader,
  1852. * then don't put it on unless the group is on.
  1853. */
  1854. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  1855. goto unlock;
  1856. if (!group_can_go_on(event, cpuctx, 1)) {
  1857. err = -EEXIST;
  1858. } else {
  1859. if (event == leader)
  1860. err = group_sched_in(event, cpuctx, ctx);
  1861. else
  1862. err = event_sched_in(event, cpuctx, ctx);
  1863. }
  1864. if (err) {
  1865. /*
  1866. * If this event can't go on and it's part of a
  1867. * group, then the whole group has to come off.
  1868. */
  1869. if (leader != event) {
  1870. group_sched_out(leader, cpuctx, ctx);
  1871. perf_mux_hrtimer_restart(cpuctx);
  1872. }
  1873. if (leader->attr.pinned) {
  1874. update_group_times(leader);
  1875. leader->state = PERF_EVENT_STATE_ERROR;
  1876. }
  1877. }
  1878. unlock:
  1879. raw_spin_unlock(&ctx->lock);
  1880. return 0;
  1881. }
  1882. /*
  1883. * Enable a event.
  1884. *
  1885. * If event->ctx is a cloned context, callers must make sure that
  1886. * every task struct that event->ctx->task could possibly point to
  1887. * remains valid. This condition is satisfied when called through
  1888. * perf_event_for_each_child or perf_event_for_each as described
  1889. * for perf_event_disable.
  1890. */
  1891. static void _perf_event_enable(struct perf_event *event)
  1892. {
  1893. struct perf_event_context *ctx = event->ctx;
  1894. struct task_struct *task = ctx->task;
  1895. if (!task) {
  1896. /*
  1897. * Enable the event on the cpu that it's on
  1898. */
  1899. cpu_function_call(event->cpu, __perf_event_enable, event);
  1900. return;
  1901. }
  1902. raw_spin_lock_irq(&ctx->lock);
  1903. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1904. goto out;
  1905. /*
  1906. * If the event is in error state, clear that first.
  1907. * That way, if we see the event in error state below, we
  1908. * know that it has gone back into error state, as distinct
  1909. * from the task having been scheduled away before the
  1910. * cross-call arrived.
  1911. */
  1912. if (event->state == PERF_EVENT_STATE_ERROR)
  1913. event->state = PERF_EVENT_STATE_OFF;
  1914. retry:
  1915. if (!ctx->is_active) {
  1916. __perf_event_mark_enabled(event);
  1917. goto out;
  1918. }
  1919. raw_spin_unlock_irq(&ctx->lock);
  1920. if (!task_function_call(task, __perf_event_enable, event))
  1921. return;
  1922. raw_spin_lock_irq(&ctx->lock);
  1923. /*
  1924. * If the context is active and the event is still off,
  1925. * we need to retry the cross-call.
  1926. */
  1927. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
  1928. /*
  1929. * task could have been flipped by a concurrent
  1930. * perf_event_context_sched_out()
  1931. */
  1932. task = ctx->task;
  1933. goto retry;
  1934. }
  1935. out:
  1936. raw_spin_unlock_irq(&ctx->lock);
  1937. }
  1938. /*
  1939. * See perf_event_disable();
  1940. */
  1941. void perf_event_enable(struct perf_event *event)
  1942. {
  1943. struct perf_event_context *ctx;
  1944. ctx = perf_event_ctx_lock(event);
  1945. _perf_event_enable(event);
  1946. perf_event_ctx_unlock(event, ctx);
  1947. }
  1948. EXPORT_SYMBOL_GPL(perf_event_enable);
  1949. static int _perf_event_refresh(struct perf_event *event, int refresh)
  1950. {
  1951. /*
  1952. * not supported on inherited events
  1953. */
  1954. if (event->attr.inherit || !is_sampling_event(event))
  1955. return -EINVAL;
  1956. atomic_add(refresh, &event->event_limit);
  1957. _perf_event_enable(event);
  1958. return 0;
  1959. }
  1960. /*
  1961. * See perf_event_disable()
  1962. */
  1963. int perf_event_refresh(struct perf_event *event, int refresh)
  1964. {
  1965. struct perf_event_context *ctx;
  1966. int ret;
  1967. ctx = perf_event_ctx_lock(event);
  1968. ret = _perf_event_refresh(event, refresh);
  1969. perf_event_ctx_unlock(event, ctx);
  1970. return ret;
  1971. }
  1972. EXPORT_SYMBOL_GPL(perf_event_refresh);
  1973. static void ctx_sched_out(struct perf_event_context *ctx,
  1974. struct perf_cpu_context *cpuctx,
  1975. enum event_type_t event_type)
  1976. {
  1977. struct perf_event *event;
  1978. int is_active = ctx->is_active;
  1979. ctx->is_active &= ~event_type;
  1980. if (likely(!ctx->nr_events))
  1981. return;
  1982. update_context_time(ctx);
  1983. update_cgrp_time_from_cpuctx(cpuctx);
  1984. if (!ctx->nr_active)
  1985. return;
  1986. perf_pmu_disable(ctx->pmu);
  1987. if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
  1988. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  1989. group_sched_out(event, cpuctx, ctx);
  1990. }
  1991. if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
  1992. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  1993. group_sched_out(event, cpuctx, ctx);
  1994. }
  1995. perf_pmu_enable(ctx->pmu);
  1996. }
  1997. /*
  1998. * Test whether two contexts are equivalent, i.e. whether they have both been
  1999. * cloned from the same version of the same context.
  2000. *
  2001. * Equivalence is measured using a generation number in the context that is
  2002. * incremented on each modification to it; see unclone_ctx(), list_add_event()
  2003. * and list_del_event().
  2004. */
  2005. static int context_equiv(struct perf_event_context *ctx1,
  2006. struct perf_event_context *ctx2)
  2007. {
  2008. lockdep_assert_held(&ctx1->lock);
  2009. lockdep_assert_held(&ctx2->lock);
  2010. /* Pinning disables the swap optimization */
  2011. if (ctx1->pin_count || ctx2->pin_count)
  2012. return 0;
  2013. /* If ctx1 is the parent of ctx2 */
  2014. if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
  2015. return 1;
  2016. /* If ctx2 is the parent of ctx1 */
  2017. if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
  2018. return 1;
  2019. /*
  2020. * If ctx1 and ctx2 have the same parent; we flatten the parent
  2021. * hierarchy, see perf_event_init_context().
  2022. */
  2023. if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
  2024. ctx1->parent_gen == ctx2->parent_gen)
  2025. return 1;
  2026. /* Unmatched */
  2027. return 0;
  2028. }
  2029. static void __perf_event_sync_stat(struct perf_event *event,
  2030. struct perf_event *next_event)
  2031. {
  2032. u64 value;
  2033. if (!event->attr.inherit_stat)
  2034. return;
  2035. /*
  2036. * Update the event value, we cannot use perf_event_read()
  2037. * because we're in the middle of a context switch and have IRQs
  2038. * disabled, which upsets smp_call_function_single(), however
  2039. * we know the event must be on the current CPU, therefore we
  2040. * don't need to use it.
  2041. */
  2042. switch (event->state) {
  2043. case PERF_EVENT_STATE_ACTIVE:
  2044. event->pmu->read(event);
  2045. /* fall-through */
  2046. case PERF_EVENT_STATE_INACTIVE:
  2047. update_event_times(event);
  2048. break;
  2049. default:
  2050. break;
  2051. }
  2052. /*
  2053. * In order to keep per-task stats reliable we need to flip the event
  2054. * values when we flip the contexts.
  2055. */
  2056. value = local64_read(&next_event->count);
  2057. value = local64_xchg(&event->count, value);
  2058. local64_set(&next_event->count, value);
  2059. swap(event->total_time_enabled, next_event->total_time_enabled);
  2060. swap(event->total_time_running, next_event->total_time_running);
  2061. /*
  2062. * Since we swizzled the values, update the user visible data too.
  2063. */
  2064. perf_event_update_userpage(event);
  2065. perf_event_update_userpage(next_event);
  2066. }
  2067. static void perf_event_sync_stat(struct perf_event_context *ctx,
  2068. struct perf_event_context *next_ctx)
  2069. {
  2070. struct perf_event *event, *next_event;
  2071. if (!ctx->nr_stat)
  2072. return;
  2073. update_context_time(ctx);
  2074. event = list_first_entry(&ctx->event_list,
  2075. struct perf_event, event_entry);
  2076. next_event = list_first_entry(&next_ctx->event_list,
  2077. struct perf_event, event_entry);
  2078. while (&event->event_entry != &ctx->event_list &&
  2079. &next_event->event_entry != &next_ctx->event_list) {
  2080. __perf_event_sync_stat(event, next_event);
  2081. event = list_next_entry(event, event_entry);
  2082. next_event = list_next_entry(next_event, event_entry);
  2083. }
  2084. }
  2085. static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  2086. struct task_struct *next)
  2087. {
  2088. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  2089. struct perf_event_context *next_ctx;
  2090. struct perf_event_context *parent, *next_parent;
  2091. struct perf_cpu_context *cpuctx;
  2092. int do_switch = 1;
  2093. if (likely(!ctx))
  2094. return;
  2095. cpuctx = __get_cpu_context(ctx);
  2096. if (!cpuctx->task_ctx)
  2097. return;
  2098. rcu_read_lock();
  2099. next_ctx = next->perf_event_ctxp[ctxn];
  2100. if (!next_ctx)
  2101. goto unlock;
  2102. parent = rcu_dereference(ctx->parent_ctx);
  2103. next_parent = rcu_dereference(next_ctx->parent_ctx);
  2104. /* If neither context have a parent context; they cannot be clones. */
  2105. if (!parent && !next_parent)
  2106. goto unlock;
  2107. if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
  2108. /*
  2109. * Looks like the two contexts are clones, so we might be
  2110. * able to optimize the context switch. We lock both
  2111. * contexts and check that they are clones under the
  2112. * lock (including re-checking that neither has been
  2113. * uncloned in the meantime). It doesn't matter which
  2114. * order we take the locks because no other cpu could
  2115. * be trying to lock both of these tasks.
  2116. */
  2117. raw_spin_lock(&ctx->lock);
  2118. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  2119. if (context_equiv(ctx, next_ctx)) {
  2120. /*
  2121. * XXX do we need a memory barrier of sorts
  2122. * wrt to rcu_dereference() of perf_event_ctxp
  2123. */
  2124. task->perf_event_ctxp[ctxn] = next_ctx;
  2125. next->perf_event_ctxp[ctxn] = ctx;
  2126. ctx->task = next;
  2127. next_ctx->task = task;
  2128. swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
  2129. do_switch = 0;
  2130. perf_event_sync_stat(ctx, next_ctx);
  2131. }
  2132. raw_spin_unlock(&next_ctx->lock);
  2133. raw_spin_unlock(&ctx->lock);
  2134. }
  2135. unlock:
  2136. rcu_read_unlock();
  2137. if (do_switch) {
  2138. raw_spin_lock(&ctx->lock);
  2139. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  2140. cpuctx->task_ctx = NULL;
  2141. raw_spin_unlock(&ctx->lock);
  2142. }
  2143. }
  2144. void perf_sched_cb_dec(struct pmu *pmu)
  2145. {
  2146. this_cpu_dec(perf_sched_cb_usages);
  2147. }
  2148. void perf_sched_cb_inc(struct pmu *pmu)
  2149. {
  2150. this_cpu_inc(perf_sched_cb_usages);
  2151. }
  2152. /*
  2153. * This function provides the context switch callback to the lower code
  2154. * layer. It is invoked ONLY when the context switch callback is enabled.
  2155. */
  2156. static void perf_pmu_sched_task(struct task_struct *prev,
  2157. struct task_struct *next,
  2158. bool sched_in)
  2159. {
  2160. struct perf_cpu_context *cpuctx;
  2161. struct pmu *pmu;
  2162. unsigned long flags;
  2163. if (prev == next)
  2164. return;
  2165. local_irq_save(flags);
  2166. rcu_read_lock();
  2167. list_for_each_entry_rcu(pmu, &pmus, entry) {
  2168. if (pmu->sched_task) {
  2169. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  2170. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2171. perf_pmu_disable(pmu);
  2172. pmu->sched_task(cpuctx->task_ctx, sched_in);
  2173. perf_pmu_enable(pmu);
  2174. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2175. }
  2176. }
  2177. rcu_read_unlock();
  2178. local_irq_restore(flags);
  2179. }
  2180. #define for_each_task_context_nr(ctxn) \
  2181. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  2182. /*
  2183. * Called from scheduler to remove the events of the current task,
  2184. * with interrupts disabled.
  2185. *
  2186. * We stop each event and update the event value in event->count.
  2187. *
  2188. * This does not protect us against NMI, but disable()
  2189. * sets the disabled bit in the control field of event _before_
  2190. * accessing the event control register. If a NMI hits, then it will
  2191. * not restart the event.
  2192. */
  2193. void __perf_event_task_sched_out(struct task_struct *task,
  2194. struct task_struct *next)
  2195. {
  2196. int ctxn;
  2197. if (__this_cpu_read(perf_sched_cb_usages))
  2198. perf_pmu_sched_task(task, next, false);
  2199. for_each_task_context_nr(ctxn)
  2200. perf_event_context_sched_out(task, ctxn, next);
  2201. /*
  2202. * if cgroup events exist on this CPU, then we need
  2203. * to check if we have to switch out PMU state.
  2204. * cgroup event are system-wide mode only
  2205. */
  2206. if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
  2207. perf_cgroup_sched_out(task, next);
  2208. }
  2209. static void task_ctx_sched_out(struct perf_event_context *ctx)
  2210. {
  2211. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  2212. if (!cpuctx->task_ctx)
  2213. return;
  2214. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  2215. return;
  2216. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  2217. cpuctx->task_ctx = NULL;
  2218. }
  2219. /*
  2220. * Called with IRQs disabled
  2221. */
  2222. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  2223. enum event_type_t event_type)
  2224. {
  2225. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  2226. }
  2227. static void
  2228. ctx_pinned_sched_in(struct perf_event_context *ctx,
  2229. struct perf_cpu_context *cpuctx)
  2230. {
  2231. struct perf_event *event;
  2232. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  2233. if (event->state <= PERF_EVENT_STATE_OFF)
  2234. continue;
  2235. if (!event_filter_match(event))
  2236. continue;
  2237. /* may need to reset tstamp_enabled */
  2238. if (is_cgroup_event(event))
  2239. perf_cgroup_mark_enabled(event, ctx);
  2240. if (group_can_go_on(event, cpuctx, 1))
  2241. group_sched_in(event, cpuctx, ctx);
  2242. /*
  2243. * If this pinned group hasn't been scheduled,
  2244. * put it in error state.
  2245. */
  2246. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2247. update_group_times(event);
  2248. event->state = PERF_EVENT_STATE_ERROR;
  2249. }
  2250. }
  2251. }
  2252. static void
  2253. ctx_flexible_sched_in(struct perf_event_context *ctx,
  2254. struct perf_cpu_context *cpuctx)
  2255. {
  2256. struct perf_event *event;
  2257. int can_add_hw = 1;
  2258. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  2259. /* Ignore events in OFF or ERROR state */
  2260. if (event->state <= PERF_EVENT_STATE_OFF)
  2261. continue;
  2262. /*
  2263. * Listen to the 'cpu' scheduling filter constraint
  2264. * of events:
  2265. */
  2266. if (!event_filter_match(event))
  2267. continue;
  2268. /* may need to reset tstamp_enabled */
  2269. if (is_cgroup_event(event))
  2270. perf_cgroup_mark_enabled(event, ctx);
  2271. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  2272. if (group_sched_in(event, cpuctx, ctx))
  2273. can_add_hw = 0;
  2274. }
  2275. }
  2276. }
  2277. static void
  2278. ctx_sched_in(struct perf_event_context *ctx,
  2279. struct perf_cpu_context *cpuctx,
  2280. enum event_type_t event_type,
  2281. struct task_struct *task)
  2282. {
  2283. u64 now;
  2284. int is_active = ctx->is_active;
  2285. ctx->is_active |= event_type;
  2286. if (likely(!ctx->nr_events))
  2287. return;
  2288. now = perf_clock();
  2289. ctx->timestamp = now;
  2290. perf_cgroup_set_timestamp(task, ctx);
  2291. /*
  2292. * First go through the list and put on any pinned groups
  2293. * in order to give them the best chance of going on.
  2294. */
  2295. if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
  2296. ctx_pinned_sched_in(ctx, cpuctx);
  2297. /* Then walk through the lower prio flexible groups */
  2298. if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
  2299. ctx_flexible_sched_in(ctx, cpuctx);
  2300. }
  2301. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  2302. enum event_type_t event_type,
  2303. struct task_struct *task)
  2304. {
  2305. struct perf_event_context *ctx = &cpuctx->ctx;
  2306. ctx_sched_in(ctx, cpuctx, event_type, task);
  2307. }
  2308. static void perf_event_context_sched_in(struct perf_event_context *ctx,
  2309. struct task_struct *task)
  2310. {
  2311. struct perf_cpu_context *cpuctx;
  2312. cpuctx = __get_cpu_context(ctx);
  2313. if (cpuctx->task_ctx == ctx)
  2314. return;
  2315. perf_ctx_lock(cpuctx, ctx);
  2316. perf_pmu_disable(ctx->pmu);
  2317. /*
  2318. * We want to keep the following priority order:
  2319. * cpu pinned (that don't need to move), task pinned,
  2320. * cpu flexible, task flexible.
  2321. */
  2322. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2323. if (ctx->nr_events)
  2324. cpuctx->task_ctx = ctx;
  2325. perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
  2326. perf_pmu_enable(ctx->pmu);
  2327. perf_ctx_unlock(cpuctx, ctx);
  2328. }
  2329. /*
  2330. * Called from scheduler to add the events of the current task
  2331. * with interrupts disabled.
  2332. *
  2333. * We restore the event value and then enable it.
  2334. *
  2335. * This does not protect us against NMI, but enable()
  2336. * sets the enabled bit in the control field of event _before_
  2337. * accessing the event control register. If a NMI hits, then it will
  2338. * keep the event running.
  2339. */
  2340. void __perf_event_task_sched_in(struct task_struct *prev,
  2341. struct task_struct *task)
  2342. {
  2343. struct perf_event_context *ctx;
  2344. int ctxn;
  2345. for_each_task_context_nr(ctxn) {
  2346. ctx = task->perf_event_ctxp[ctxn];
  2347. if (likely(!ctx))
  2348. continue;
  2349. perf_event_context_sched_in(ctx, task);
  2350. }
  2351. /*
  2352. * if cgroup events exist on this CPU, then we need
  2353. * to check if we have to switch in PMU state.
  2354. * cgroup event are system-wide mode only
  2355. */
  2356. if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
  2357. perf_cgroup_sched_in(prev, task);
  2358. if (__this_cpu_read(perf_sched_cb_usages))
  2359. perf_pmu_sched_task(prev, task, true);
  2360. }
  2361. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  2362. {
  2363. u64 frequency = event->attr.sample_freq;
  2364. u64 sec = NSEC_PER_SEC;
  2365. u64 divisor, dividend;
  2366. int count_fls, nsec_fls, frequency_fls, sec_fls;
  2367. count_fls = fls64(count);
  2368. nsec_fls = fls64(nsec);
  2369. frequency_fls = fls64(frequency);
  2370. sec_fls = 30;
  2371. /*
  2372. * We got @count in @nsec, with a target of sample_freq HZ
  2373. * the target period becomes:
  2374. *
  2375. * @count * 10^9
  2376. * period = -------------------
  2377. * @nsec * sample_freq
  2378. *
  2379. */
  2380. /*
  2381. * Reduce accuracy by one bit such that @a and @b converge
  2382. * to a similar magnitude.
  2383. */
  2384. #define REDUCE_FLS(a, b) \
  2385. do { \
  2386. if (a##_fls > b##_fls) { \
  2387. a >>= 1; \
  2388. a##_fls--; \
  2389. } else { \
  2390. b >>= 1; \
  2391. b##_fls--; \
  2392. } \
  2393. } while (0)
  2394. /*
  2395. * Reduce accuracy until either term fits in a u64, then proceed with
  2396. * the other, so that finally we can do a u64/u64 division.
  2397. */
  2398. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  2399. REDUCE_FLS(nsec, frequency);
  2400. REDUCE_FLS(sec, count);
  2401. }
  2402. if (count_fls + sec_fls > 64) {
  2403. divisor = nsec * frequency;
  2404. while (count_fls + sec_fls > 64) {
  2405. REDUCE_FLS(count, sec);
  2406. divisor >>= 1;
  2407. }
  2408. dividend = count * sec;
  2409. } else {
  2410. dividend = count * sec;
  2411. while (nsec_fls + frequency_fls > 64) {
  2412. REDUCE_FLS(nsec, frequency);
  2413. dividend >>= 1;
  2414. }
  2415. divisor = nsec * frequency;
  2416. }
  2417. if (!divisor)
  2418. return dividend;
  2419. return div64_u64(dividend, divisor);
  2420. }
  2421. static DEFINE_PER_CPU(int, perf_throttled_count);
  2422. static DEFINE_PER_CPU(u64, perf_throttled_seq);
  2423. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
  2424. {
  2425. struct hw_perf_event *hwc = &event->hw;
  2426. s64 period, sample_period;
  2427. s64 delta;
  2428. period = perf_calculate_period(event, nsec, count);
  2429. delta = (s64)(period - hwc->sample_period);
  2430. delta = (delta + 7) / 8; /* low pass filter */
  2431. sample_period = hwc->sample_period + delta;
  2432. if (!sample_period)
  2433. sample_period = 1;
  2434. hwc->sample_period = sample_period;
  2435. if (local64_read(&hwc->period_left) > 8*sample_period) {
  2436. if (disable)
  2437. event->pmu->stop(event, PERF_EF_UPDATE);
  2438. local64_set(&hwc->period_left, 0);
  2439. if (disable)
  2440. event->pmu->start(event, PERF_EF_RELOAD);
  2441. }
  2442. }
  2443. /*
  2444. * combine freq adjustment with unthrottling to avoid two passes over the
  2445. * events. At the same time, make sure, having freq events does not change
  2446. * the rate of unthrottling as that would introduce bias.
  2447. */
  2448. static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
  2449. int needs_unthr)
  2450. {
  2451. struct perf_event *event;
  2452. struct hw_perf_event *hwc;
  2453. u64 now, period = TICK_NSEC;
  2454. s64 delta;
  2455. /*
  2456. * only need to iterate over all events iff:
  2457. * - context have events in frequency mode (needs freq adjust)
  2458. * - there are events to unthrottle on this cpu
  2459. */
  2460. if (!(ctx->nr_freq || needs_unthr))
  2461. return;
  2462. raw_spin_lock(&ctx->lock);
  2463. perf_pmu_disable(ctx->pmu);
  2464. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2465. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2466. continue;
  2467. if (!event_filter_match(event))
  2468. continue;
  2469. perf_pmu_disable(event->pmu);
  2470. hwc = &event->hw;
  2471. if (hwc->interrupts == MAX_INTERRUPTS) {
  2472. hwc->interrupts = 0;
  2473. perf_log_throttle(event, 1);
  2474. event->pmu->start(event, 0);
  2475. }
  2476. if (!event->attr.freq || !event->attr.sample_freq)
  2477. goto next;
  2478. /*
  2479. * stop the event and update event->count
  2480. */
  2481. event->pmu->stop(event, PERF_EF_UPDATE);
  2482. now = local64_read(&event->count);
  2483. delta = now - hwc->freq_count_stamp;
  2484. hwc->freq_count_stamp = now;
  2485. /*
  2486. * restart the event
  2487. * reload only if value has changed
  2488. * we have stopped the event so tell that
  2489. * to perf_adjust_period() to avoid stopping it
  2490. * twice.
  2491. */
  2492. if (delta > 0)
  2493. perf_adjust_period(event, period, delta, false);
  2494. event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
  2495. next:
  2496. perf_pmu_enable(event->pmu);
  2497. }
  2498. perf_pmu_enable(ctx->pmu);
  2499. raw_spin_unlock(&ctx->lock);
  2500. }
  2501. /*
  2502. * Round-robin a context's events:
  2503. */
  2504. static void rotate_ctx(struct perf_event_context *ctx)
  2505. {
  2506. /*
  2507. * Rotate the first entry last of non-pinned groups. Rotation might be
  2508. * disabled by the inheritance code.
  2509. */
  2510. if (!ctx->rotate_disable)
  2511. list_rotate_left(&ctx->flexible_groups);
  2512. }
  2513. static int perf_rotate_context(struct perf_cpu_context *cpuctx)
  2514. {
  2515. struct perf_event_context *ctx = NULL;
  2516. int rotate = 0;
  2517. if (cpuctx->ctx.nr_events) {
  2518. if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  2519. rotate = 1;
  2520. }
  2521. ctx = cpuctx->task_ctx;
  2522. if (ctx && ctx->nr_events) {
  2523. if (ctx->nr_events != ctx->nr_active)
  2524. rotate = 1;
  2525. }
  2526. if (!rotate)
  2527. goto done;
  2528. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2529. perf_pmu_disable(cpuctx->ctx.pmu);
  2530. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2531. if (ctx)
  2532. ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
  2533. rotate_ctx(&cpuctx->ctx);
  2534. if (ctx)
  2535. rotate_ctx(ctx);
  2536. perf_event_sched_in(cpuctx, ctx, current);
  2537. perf_pmu_enable(cpuctx->ctx.pmu);
  2538. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2539. done:
  2540. return rotate;
  2541. }
  2542. #ifdef CONFIG_NO_HZ_FULL
  2543. bool perf_event_can_stop_tick(void)
  2544. {
  2545. if (atomic_read(&nr_freq_events) ||
  2546. __this_cpu_read(perf_throttled_count))
  2547. return false;
  2548. else
  2549. return true;
  2550. }
  2551. #endif
  2552. void perf_event_task_tick(void)
  2553. {
  2554. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  2555. struct perf_event_context *ctx, *tmp;
  2556. int throttled;
  2557. WARN_ON(!irqs_disabled());
  2558. __this_cpu_inc(perf_throttled_seq);
  2559. throttled = __this_cpu_xchg(perf_throttled_count, 0);
  2560. list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
  2561. perf_adjust_freq_unthr_context(ctx, throttled);
  2562. }
  2563. static int event_enable_on_exec(struct perf_event *event,
  2564. struct perf_event_context *ctx)
  2565. {
  2566. if (!event->attr.enable_on_exec)
  2567. return 0;
  2568. event->attr.enable_on_exec = 0;
  2569. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  2570. return 0;
  2571. __perf_event_mark_enabled(event);
  2572. return 1;
  2573. }
  2574. /*
  2575. * Enable all of a task's events that have been marked enable-on-exec.
  2576. * This expects task == current.
  2577. */
  2578. static void perf_event_enable_on_exec(struct perf_event_context *ctx)
  2579. {
  2580. struct perf_event_context *clone_ctx = NULL;
  2581. struct perf_event *event;
  2582. unsigned long flags;
  2583. int enabled = 0;
  2584. int ret;
  2585. local_irq_save(flags);
  2586. if (!ctx || !ctx->nr_events)
  2587. goto out;
  2588. /*
  2589. * We must ctxsw out cgroup events to avoid conflict
  2590. * when invoking perf_task_event_sched_in() later on
  2591. * in this function. Otherwise we end up trying to
  2592. * ctxswin cgroup events which are already scheduled
  2593. * in.
  2594. */
  2595. perf_cgroup_sched_out(current, NULL);
  2596. raw_spin_lock(&ctx->lock);
  2597. task_ctx_sched_out(ctx);
  2598. list_for_each_entry(event, &ctx->event_list, event_entry) {
  2599. ret = event_enable_on_exec(event, ctx);
  2600. if (ret)
  2601. enabled = 1;
  2602. }
  2603. /*
  2604. * Unclone this context if we enabled any event.
  2605. */
  2606. if (enabled)
  2607. clone_ctx = unclone_ctx(ctx);
  2608. raw_spin_unlock(&ctx->lock);
  2609. /*
  2610. * Also calls ctxswin for cgroup events, if any:
  2611. */
  2612. perf_event_context_sched_in(ctx, ctx->task);
  2613. out:
  2614. local_irq_restore(flags);
  2615. if (clone_ctx)
  2616. put_ctx(clone_ctx);
  2617. }
  2618. void perf_event_exec(void)
  2619. {
  2620. struct perf_event_context *ctx;
  2621. int ctxn;
  2622. rcu_read_lock();
  2623. for_each_task_context_nr(ctxn) {
  2624. ctx = current->perf_event_ctxp[ctxn];
  2625. if (!ctx)
  2626. continue;
  2627. perf_event_enable_on_exec(ctx);
  2628. }
  2629. rcu_read_unlock();
  2630. }
  2631. /*
  2632. * Cross CPU call to read the hardware event
  2633. */
  2634. static void __perf_event_read(void *info)
  2635. {
  2636. struct perf_event *event = info;
  2637. struct perf_event_context *ctx = event->ctx;
  2638. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  2639. /*
  2640. * If this is a task context, we need to check whether it is
  2641. * the current task context of this cpu. If not it has been
  2642. * scheduled out before the smp call arrived. In that case
  2643. * event->count would have been updated to a recent sample
  2644. * when the event was scheduled out.
  2645. */
  2646. if (ctx->task && cpuctx->task_ctx != ctx)
  2647. return;
  2648. raw_spin_lock(&ctx->lock);
  2649. if (ctx->is_active) {
  2650. update_context_time(ctx);
  2651. update_cgrp_time_from_event(event);
  2652. }
  2653. update_event_times(event);
  2654. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2655. event->pmu->read(event);
  2656. raw_spin_unlock(&ctx->lock);
  2657. }
  2658. static inline u64 perf_event_count(struct perf_event *event)
  2659. {
  2660. if (event->pmu->count)
  2661. return event->pmu->count(event);
  2662. return __perf_event_count(event);
  2663. }
  2664. static u64 perf_event_read(struct perf_event *event)
  2665. {
  2666. /*
  2667. * If event is enabled and currently active on a CPU, update the
  2668. * value in the event structure:
  2669. */
  2670. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  2671. smp_call_function_single(event->oncpu,
  2672. __perf_event_read, event, 1);
  2673. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2674. struct perf_event_context *ctx = event->ctx;
  2675. unsigned long flags;
  2676. raw_spin_lock_irqsave(&ctx->lock, flags);
  2677. /*
  2678. * may read while context is not active
  2679. * (e.g., thread is blocked), in that case
  2680. * we cannot update context time
  2681. */
  2682. if (ctx->is_active) {
  2683. update_context_time(ctx);
  2684. update_cgrp_time_from_event(event);
  2685. }
  2686. update_event_times(event);
  2687. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2688. }
  2689. return perf_event_count(event);
  2690. }
  2691. /*
  2692. * Initialize the perf_event context in a task_struct:
  2693. */
  2694. static void __perf_event_init_context(struct perf_event_context *ctx)
  2695. {
  2696. raw_spin_lock_init(&ctx->lock);
  2697. mutex_init(&ctx->mutex);
  2698. INIT_LIST_HEAD(&ctx->active_ctx_list);
  2699. INIT_LIST_HEAD(&ctx->pinned_groups);
  2700. INIT_LIST_HEAD(&ctx->flexible_groups);
  2701. INIT_LIST_HEAD(&ctx->event_list);
  2702. atomic_set(&ctx->refcount, 1);
  2703. INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
  2704. }
  2705. static struct perf_event_context *
  2706. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  2707. {
  2708. struct perf_event_context *ctx;
  2709. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  2710. if (!ctx)
  2711. return NULL;
  2712. __perf_event_init_context(ctx);
  2713. if (task) {
  2714. ctx->task = task;
  2715. get_task_struct(task);
  2716. }
  2717. ctx->pmu = pmu;
  2718. return ctx;
  2719. }
  2720. static struct task_struct *
  2721. find_lively_task_by_vpid(pid_t vpid)
  2722. {
  2723. struct task_struct *task;
  2724. int err;
  2725. rcu_read_lock();
  2726. if (!vpid)
  2727. task = current;
  2728. else
  2729. task = find_task_by_vpid(vpid);
  2730. if (task)
  2731. get_task_struct(task);
  2732. rcu_read_unlock();
  2733. if (!task)
  2734. return ERR_PTR(-ESRCH);
  2735. /* Reuse ptrace permission checks for now. */
  2736. err = -EACCES;
  2737. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  2738. goto errout;
  2739. return task;
  2740. errout:
  2741. put_task_struct(task);
  2742. return ERR_PTR(err);
  2743. }
  2744. /*
  2745. * Returns a matching context with refcount and pincount.
  2746. */
  2747. static struct perf_event_context *
  2748. find_get_context(struct pmu *pmu, struct task_struct *task,
  2749. struct perf_event *event)
  2750. {
  2751. struct perf_event_context *ctx, *clone_ctx = NULL;
  2752. struct perf_cpu_context *cpuctx;
  2753. void *task_ctx_data = NULL;
  2754. unsigned long flags;
  2755. int ctxn, err;
  2756. int cpu = event->cpu;
  2757. if (!task) {
  2758. /* Must be root to operate on a CPU event: */
  2759. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  2760. return ERR_PTR(-EACCES);
  2761. /*
  2762. * We could be clever and allow to attach a event to an
  2763. * offline CPU and activate it when the CPU comes up, but
  2764. * that's for later.
  2765. */
  2766. if (!cpu_online(cpu))
  2767. return ERR_PTR(-ENODEV);
  2768. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  2769. ctx = &cpuctx->ctx;
  2770. get_ctx(ctx);
  2771. ++ctx->pin_count;
  2772. return ctx;
  2773. }
  2774. err = -EINVAL;
  2775. ctxn = pmu->task_ctx_nr;
  2776. if (ctxn < 0)
  2777. goto errout;
  2778. if (event->attach_state & PERF_ATTACH_TASK_DATA) {
  2779. task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
  2780. if (!task_ctx_data) {
  2781. err = -ENOMEM;
  2782. goto errout;
  2783. }
  2784. }
  2785. retry:
  2786. ctx = perf_lock_task_context(task, ctxn, &flags);
  2787. if (ctx) {
  2788. clone_ctx = unclone_ctx(ctx);
  2789. ++ctx->pin_count;
  2790. if (task_ctx_data && !ctx->task_ctx_data) {
  2791. ctx->task_ctx_data = task_ctx_data;
  2792. task_ctx_data = NULL;
  2793. }
  2794. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2795. if (clone_ctx)
  2796. put_ctx(clone_ctx);
  2797. } else {
  2798. ctx = alloc_perf_context(pmu, task);
  2799. err = -ENOMEM;
  2800. if (!ctx)
  2801. goto errout;
  2802. if (task_ctx_data) {
  2803. ctx->task_ctx_data = task_ctx_data;
  2804. task_ctx_data = NULL;
  2805. }
  2806. err = 0;
  2807. mutex_lock(&task->perf_event_mutex);
  2808. /*
  2809. * If it has already passed perf_event_exit_task().
  2810. * we must see PF_EXITING, it takes this mutex too.
  2811. */
  2812. if (task->flags & PF_EXITING)
  2813. err = -ESRCH;
  2814. else if (task->perf_event_ctxp[ctxn])
  2815. err = -EAGAIN;
  2816. else {
  2817. get_ctx(ctx);
  2818. ++ctx->pin_count;
  2819. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  2820. }
  2821. mutex_unlock(&task->perf_event_mutex);
  2822. if (unlikely(err)) {
  2823. put_ctx(ctx);
  2824. if (err == -EAGAIN)
  2825. goto retry;
  2826. goto errout;
  2827. }
  2828. }
  2829. kfree(task_ctx_data);
  2830. return ctx;
  2831. errout:
  2832. kfree(task_ctx_data);
  2833. return ERR_PTR(err);
  2834. }
  2835. static void perf_event_free_filter(struct perf_event *event);
  2836. static void perf_event_free_bpf_prog(struct perf_event *event);
  2837. static void free_event_rcu(struct rcu_head *head)
  2838. {
  2839. struct perf_event *event;
  2840. event = container_of(head, struct perf_event, rcu_head);
  2841. if (event->ns)
  2842. put_pid_ns(event->ns);
  2843. perf_event_free_filter(event);
  2844. kfree(event);
  2845. }
  2846. static void ring_buffer_attach(struct perf_event *event,
  2847. struct ring_buffer *rb);
  2848. static void unaccount_event_cpu(struct perf_event *event, int cpu)
  2849. {
  2850. if (event->parent)
  2851. return;
  2852. if (is_cgroup_event(event))
  2853. atomic_dec(&per_cpu(perf_cgroup_events, cpu));
  2854. }
  2855. static void unaccount_event(struct perf_event *event)
  2856. {
  2857. if (event->parent)
  2858. return;
  2859. if (event->attach_state & PERF_ATTACH_TASK)
  2860. static_key_slow_dec_deferred(&perf_sched_events);
  2861. if (event->attr.mmap || event->attr.mmap_data)
  2862. atomic_dec(&nr_mmap_events);
  2863. if (event->attr.comm)
  2864. atomic_dec(&nr_comm_events);
  2865. if (event->attr.task)
  2866. atomic_dec(&nr_task_events);
  2867. if (event->attr.freq)
  2868. atomic_dec(&nr_freq_events);
  2869. if (is_cgroup_event(event))
  2870. static_key_slow_dec_deferred(&perf_sched_events);
  2871. if (has_branch_stack(event))
  2872. static_key_slow_dec_deferred(&perf_sched_events);
  2873. unaccount_event_cpu(event, event->cpu);
  2874. }
  2875. /*
  2876. * The following implement mutual exclusion of events on "exclusive" pmus
  2877. * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
  2878. * at a time, so we disallow creating events that might conflict, namely:
  2879. *
  2880. * 1) cpu-wide events in the presence of per-task events,
  2881. * 2) per-task events in the presence of cpu-wide events,
  2882. * 3) two matching events on the same context.
  2883. *
  2884. * The former two cases are handled in the allocation path (perf_event_alloc(),
  2885. * __free_event()), the latter -- before the first perf_install_in_context().
  2886. */
  2887. static int exclusive_event_init(struct perf_event *event)
  2888. {
  2889. struct pmu *pmu = event->pmu;
  2890. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  2891. return 0;
  2892. /*
  2893. * Prevent co-existence of per-task and cpu-wide events on the
  2894. * same exclusive pmu.
  2895. *
  2896. * Negative pmu::exclusive_cnt means there are cpu-wide
  2897. * events on this "exclusive" pmu, positive means there are
  2898. * per-task events.
  2899. *
  2900. * Since this is called in perf_event_alloc() path, event::ctx
  2901. * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
  2902. * to mean "per-task event", because unlike other attach states it
  2903. * never gets cleared.
  2904. */
  2905. if (event->attach_state & PERF_ATTACH_TASK) {
  2906. if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
  2907. return -EBUSY;
  2908. } else {
  2909. if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
  2910. return -EBUSY;
  2911. }
  2912. return 0;
  2913. }
  2914. static void exclusive_event_destroy(struct perf_event *event)
  2915. {
  2916. struct pmu *pmu = event->pmu;
  2917. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  2918. return;
  2919. /* see comment in exclusive_event_init() */
  2920. if (event->attach_state & PERF_ATTACH_TASK)
  2921. atomic_dec(&pmu->exclusive_cnt);
  2922. else
  2923. atomic_inc(&pmu->exclusive_cnt);
  2924. }
  2925. static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
  2926. {
  2927. if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
  2928. (e1->cpu == e2->cpu ||
  2929. e1->cpu == -1 ||
  2930. e2->cpu == -1))
  2931. return true;
  2932. return false;
  2933. }
  2934. /* Called under the same ctx::mutex as perf_install_in_context() */
  2935. static bool exclusive_event_installable(struct perf_event *event,
  2936. struct perf_event_context *ctx)
  2937. {
  2938. struct perf_event *iter_event;
  2939. struct pmu *pmu = event->pmu;
  2940. if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
  2941. return true;
  2942. list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
  2943. if (exclusive_event_match(iter_event, event))
  2944. return false;
  2945. }
  2946. return true;
  2947. }
  2948. static void __free_event(struct perf_event *event)
  2949. {
  2950. if (!event->parent) {
  2951. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  2952. put_callchain_buffers();
  2953. }
  2954. perf_event_free_bpf_prog(event);
  2955. if (event->destroy)
  2956. event->destroy(event);
  2957. if (event->ctx)
  2958. put_ctx(event->ctx);
  2959. if (event->pmu) {
  2960. exclusive_event_destroy(event);
  2961. module_put(event->pmu->module);
  2962. }
  2963. call_rcu(&event->rcu_head, free_event_rcu);
  2964. }
  2965. static void _free_event(struct perf_event *event)
  2966. {
  2967. irq_work_sync(&event->pending);
  2968. unaccount_event(event);
  2969. if (event->rb) {
  2970. /*
  2971. * Can happen when we close an event with re-directed output.
  2972. *
  2973. * Since we have a 0 refcount, perf_mmap_close() will skip
  2974. * over us; possibly making our ring_buffer_put() the last.
  2975. */
  2976. mutex_lock(&event->mmap_mutex);
  2977. ring_buffer_attach(event, NULL);
  2978. mutex_unlock(&event->mmap_mutex);
  2979. }
  2980. if (is_cgroup_event(event))
  2981. perf_detach_cgroup(event);
  2982. __free_event(event);
  2983. }
  2984. /*
  2985. * Used to free events which have a known refcount of 1, such as in error paths
  2986. * where the event isn't exposed yet and inherited events.
  2987. */
  2988. static void free_event(struct perf_event *event)
  2989. {
  2990. if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
  2991. "unexpected event refcount: %ld; ptr=%p\n",
  2992. atomic_long_read(&event->refcount), event)) {
  2993. /* leak to avoid use-after-free */
  2994. return;
  2995. }
  2996. _free_event(event);
  2997. }
  2998. /*
  2999. * Remove user event from the owner task.
  3000. */
  3001. static void perf_remove_from_owner(struct perf_event *event)
  3002. {
  3003. struct task_struct *owner;
  3004. rcu_read_lock();
  3005. owner = ACCESS_ONCE(event->owner);
  3006. /*
  3007. * Matches the smp_wmb() in perf_event_exit_task(). If we observe
  3008. * !owner it means the list deletion is complete and we can indeed
  3009. * free this event, otherwise we need to serialize on
  3010. * owner->perf_event_mutex.
  3011. */
  3012. smp_read_barrier_depends();
  3013. if (owner) {
  3014. /*
  3015. * Since delayed_put_task_struct() also drops the last
  3016. * task reference we can safely take a new reference
  3017. * while holding the rcu_read_lock().
  3018. */
  3019. get_task_struct(owner);
  3020. }
  3021. rcu_read_unlock();
  3022. if (owner) {
  3023. /*
  3024. * If we're here through perf_event_exit_task() we're already
  3025. * holding ctx->mutex which would be an inversion wrt. the
  3026. * normal lock order.
  3027. *
  3028. * However we can safely take this lock because its the child
  3029. * ctx->mutex.
  3030. */
  3031. mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
  3032. /*
  3033. * We have to re-check the event->owner field, if it is cleared
  3034. * we raced with perf_event_exit_task(), acquiring the mutex
  3035. * ensured they're done, and we can proceed with freeing the
  3036. * event.
  3037. */
  3038. if (event->owner)
  3039. list_del_init(&event->owner_entry);
  3040. mutex_unlock(&owner->perf_event_mutex);
  3041. put_task_struct(owner);
  3042. }
  3043. }
  3044. static void put_event(struct perf_event *event)
  3045. {
  3046. struct perf_event_context *ctx;
  3047. if (!atomic_long_dec_and_test(&event->refcount))
  3048. return;
  3049. if (!is_kernel_event(event))
  3050. perf_remove_from_owner(event);
  3051. /*
  3052. * There are two ways this annotation is useful:
  3053. *
  3054. * 1) there is a lock recursion from perf_event_exit_task
  3055. * see the comment there.
  3056. *
  3057. * 2) there is a lock-inversion with mmap_sem through
  3058. * perf_event_read_group(), which takes faults while
  3059. * holding ctx->mutex, however this is called after
  3060. * the last filedesc died, so there is no possibility
  3061. * to trigger the AB-BA case.
  3062. */
  3063. ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING);
  3064. WARN_ON_ONCE(ctx->parent_ctx);
  3065. perf_remove_from_context(event, true);
  3066. perf_event_ctx_unlock(event, ctx);
  3067. _free_event(event);
  3068. }
  3069. int perf_event_release_kernel(struct perf_event *event)
  3070. {
  3071. put_event(event);
  3072. return 0;
  3073. }
  3074. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  3075. /*
  3076. * Called when the last reference to the file is gone.
  3077. */
  3078. static int perf_release(struct inode *inode, struct file *file)
  3079. {
  3080. put_event(file->private_data);
  3081. return 0;
  3082. }
  3083. /*
  3084. * Remove all orphanes events from the context.
  3085. */
  3086. static void orphans_remove_work(struct work_struct *work)
  3087. {
  3088. struct perf_event_context *ctx;
  3089. struct perf_event *event, *tmp;
  3090. ctx = container_of(work, struct perf_event_context,
  3091. orphans_remove.work);
  3092. mutex_lock(&ctx->mutex);
  3093. list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
  3094. struct perf_event *parent_event = event->parent;
  3095. if (!is_orphaned_child(event))
  3096. continue;
  3097. perf_remove_from_context(event, true);
  3098. mutex_lock(&parent_event->child_mutex);
  3099. list_del_init(&event->child_list);
  3100. mutex_unlock(&parent_event->child_mutex);
  3101. free_event(event);
  3102. put_event(parent_event);
  3103. }
  3104. raw_spin_lock_irq(&ctx->lock);
  3105. ctx->orphans_remove_sched = false;
  3106. raw_spin_unlock_irq(&ctx->lock);
  3107. mutex_unlock(&ctx->mutex);
  3108. put_ctx(ctx);
  3109. }
  3110. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  3111. {
  3112. struct perf_event *child;
  3113. u64 total = 0;
  3114. *enabled = 0;
  3115. *running = 0;
  3116. mutex_lock(&event->child_mutex);
  3117. total += perf_event_read(event);
  3118. *enabled += event->total_time_enabled +
  3119. atomic64_read(&event->child_total_time_enabled);
  3120. *running += event->total_time_running +
  3121. atomic64_read(&event->child_total_time_running);
  3122. list_for_each_entry(child, &event->child_list, child_list) {
  3123. total += perf_event_read(child);
  3124. *enabled += child->total_time_enabled;
  3125. *running += child->total_time_running;
  3126. }
  3127. mutex_unlock(&event->child_mutex);
  3128. return total;
  3129. }
  3130. EXPORT_SYMBOL_GPL(perf_event_read_value);
  3131. static int perf_event_read_group(struct perf_event *event,
  3132. u64 read_format, char __user *buf)
  3133. {
  3134. struct perf_event *leader = event->group_leader, *sub;
  3135. struct perf_event_context *ctx = leader->ctx;
  3136. int n = 0, size = 0, ret;
  3137. u64 count, enabled, running;
  3138. u64 values[5];
  3139. lockdep_assert_held(&ctx->mutex);
  3140. count = perf_event_read_value(leader, &enabled, &running);
  3141. values[n++] = 1 + leader->nr_siblings;
  3142. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3143. values[n++] = enabled;
  3144. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3145. values[n++] = running;
  3146. values[n++] = count;
  3147. if (read_format & PERF_FORMAT_ID)
  3148. values[n++] = primary_event_id(leader);
  3149. size = n * sizeof(u64);
  3150. if (copy_to_user(buf, values, size))
  3151. return -EFAULT;
  3152. ret = size;
  3153. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  3154. n = 0;
  3155. values[n++] = perf_event_read_value(sub, &enabled, &running);
  3156. if (read_format & PERF_FORMAT_ID)
  3157. values[n++] = primary_event_id(sub);
  3158. size = n * sizeof(u64);
  3159. if (copy_to_user(buf + ret, values, size)) {
  3160. return -EFAULT;
  3161. }
  3162. ret += size;
  3163. }
  3164. return ret;
  3165. }
  3166. static int perf_event_read_one(struct perf_event *event,
  3167. u64 read_format, char __user *buf)
  3168. {
  3169. u64 enabled, running;
  3170. u64 values[4];
  3171. int n = 0;
  3172. values[n++] = perf_event_read_value(event, &enabled, &running);
  3173. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3174. values[n++] = enabled;
  3175. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3176. values[n++] = running;
  3177. if (read_format & PERF_FORMAT_ID)
  3178. values[n++] = primary_event_id(event);
  3179. if (copy_to_user(buf, values, n * sizeof(u64)))
  3180. return -EFAULT;
  3181. return n * sizeof(u64);
  3182. }
  3183. static bool is_event_hup(struct perf_event *event)
  3184. {
  3185. bool no_children;
  3186. if (event->state != PERF_EVENT_STATE_EXIT)
  3187. return false;
  3188. mutex_lock(&event->child_mutex);
  3189. no_children = list_empty(&event->child_list);
  3190. mutex_unlock(&event->child_mutex);
  3191. return no_children;
  3192. }
  3193. /*
  3194. * Read the performance event - simple non blocking version for now
  3195. */
  3196. static ssize_t
  3197. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  3198. {
  3199. u64 read_format = event->attr.read_format;
  3200. int ret;
  3201. /*
  3202. * Return end-of-file for a read on a event that is in
  3203. * error state (i.e. because it was pinned but it couldn't be
  3204. * scheduled on to the CPU at some point).
  3205. */
  3206. if (event->state == PERF_EVENT_STATE_ERROR)
  3207. return 0;
  3208. if (count < event->read_size)
  3209. return -ENOSPC;
  3210. WARN_ON_ONCE(event->ctx->parent_ctx);
  3211. if (read_format & PERF_FORMAT_GROUP)
  3212. ret = perf_event_read_group(event, read_format, buf);
  3213. else
  3214. ret = perf_event_read_one(event, read_format, buf);
  3215. return ret;
  3216. }
  3217. static ssize_t
  3218. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  3219. {
  3220. struct perf_event *event = file->private_data;
  3221. struct perf_event_context *ctx;
  3222. int ret;
  3223. ctx = perf_event_ctx_lock(event);
  3224. ret = perf_read_hw(event, buf, count);
  3225. perf_event_ctx_unlock(event, ctx);
  3226. return ret;
  3227. }
  3228. static unsigned int perf_poll(struct file *file, poll_table *wait)
  3229. {
  3230. struct perf_event *event = file->private_data;
  3231. struct ring_buffer *rb;
  3232. unsigned int events = POLLHUP;
  3233. poll_wait(file, &event->waitq, wait);
  3234. if (is_event_hup(event))
  3235. return events;
  3236. /*
  3237. * Pin the event->rb by taking event->mmap_mutex; otherwise
  3238. * perf_event_set_output() can swizzle our rb and make us miss wakeups.
  3239. */
  3240. mutex_lock(&event->mmap_mutex);
  3241. rb = event->rb;
  3242. if (rb)
  3243. events = atomic_xchg(&rb->poll, 0);
  3244. mutex_unlock(&event->mmap_mutex);
  3245. return events;
  3246. }
  3247. static void _perf_event_reset(struct perf_event *event)
  3248. {
  3249. (void)perf_event_read(event);
  3250. local64_set(&event->count, 0);
  3251. perf_event_update_userpage(event);
  3252. }
  3253. /*
  3254. * Holding the top-level event's child_mutex means that any
  3255. * descendant process that has inherited this event will block
  3256. * in sync_child_event if it goes to exit, thus satisfying the
  3257. * task existence requirements of perf_event_enable/disable.
  3258. */
  3259. static void perf_event_for_each_child(struct perf_event *event,
  3260. void (*func)(struct perf_event *))
  3261. {
  3262. struct perf_event *child;
  3263. WARN_ON_ONCE(event->ctx->parent_ctx);
  3264. mutex_lock(&event->child_mutex);
  3265. func(event);
  3266. list_for_each_entry(child, &event->child_list, child_list)
  3267. func(child);
  3268. mutex_unlock(&event->child_mutex);
  3269. }
  3270. static void perf_event_for_each(struct perf_event *event,
  3271. void (*func)(struct perf_event *))
  3272. {
  3273. struct perf_event_context *ctx = event->ctx;
  3274. struct perf_event *sibling;
  3275. lockdep_assert_held(&ctx->mutex);
  3276. event = event->group_leader;
  3277. perf_event_for_each_child(event, func);
  3278. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  3279. perf_event_for_each_child(sibling, func);
  3280. }
  3281. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  3282. {
  3283. struct perf_event_context *ctx = event->ctx;
  3284. int ret = 0, active;
  3285. u64 value;
  3286. if (!is_sampling_event(event))
  3287. return -EINVAL;
  3288. if (copy_from_user(&value, arg, sizeof(value)))
  3289. return -EFAULT;
  3290. if (!value)
  3291. return -EINVAL;
  3292. raw_spin_lock_irq(&ctx->lock);
  3293. if (event->attr.freq) {
  3294. if (value > sysctl_perf_event_sample_rate) {
  3295. ret = -EINVAL;
  3296. goto unlock;
  3297. }
  3298. event->attr.sample_freq = value;
  3299. } else {
  3300. event->attr.sample_period = value;
  3301. event->hw.sample_period = value;
  3302. }
  3303. active = (event->state == PERF_EVENT_STATE_ACTIVE);
  3304. if (active) {
  3305. perf_pmu_disable(ctx->pmu);
  3306. event->pmu->stop(event, PERF_EF_UPDATE);
  3307. }
  3308. local64_set(&event->hw.period_left, 0);
  3309. if (active) {
  3310. event->pmu->start(event, PERF_EF_RELOAD);
  3311. perf_pmu_enable(ctx->pmu);
  3312. }
  3313. unlock:
  3314. raw_spin_unlock_irq(&ctx->lock);
  3315. return ret;
  3316. }
  3317. static const struct file_operations perf_fops;
  3318. static inline int perf_fget_light(int fd, struct fd *p)
  3319. {
  3320. struct fd f = fdget(fd);
  3321. if (!f.file)
  3322. return -EBADF;
  3323. if (f.file->f_op != &perf_fops) {
  3324. fdput(f);
  3325. return -EBADF;
  3326. }
  3327. *p = f;
  3328. return 0;
  3329. }
  3330. static int perf_event_set_output(struct perf_event *event,
  3331. struct perf_event *output_event);
  3332. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  3333. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
  3334. static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
  3335. {
  3336. void (*func)(struct perf_event *);
  3337. u32 flags = arg;
  3338. switch (cmd) {
  3339. case PERF_EVENT_IOC_ENABLE:
  3340. func = _perf_event_enable;
  3341. break;
  3342. case PERF_EVENT_IOC_DISABLE:
  3343. func = _perf_event_disable;
  3344. break;
  3345. case PERF_EVENT_IOC_RESET:
  3346. func = _perf_event_reset;
  3347. break;
  3348. case PERF_EVENT_IOC_REFRESH:
  3349. return _perf_event_refresh(event, arg);
  3350. case PERF_EVENT_IOC_PERIOD:
  3351. return perf_event_period(event, (u64 __user *)arg);
  3352. case PERF_EVENT_IOC_ID:
  3353. {
  3354. u64 id = primary_event_id(event);
  3355. if (copy_to_user((void __user *)arg, &id, sizeof(id)))
  3356. return -EFAULT;
  3357. return 0;
  3358. }
  3359. case PERF_EVENT_IOC_SET_OUTPUT:
  3360. {
  3361. int ret;
  3362. if (arg != -1) {
  3363. struct perf_event *output_event;
  3364. struct fd output;
  3365. ret = perf_fget_light(arg, &output);
  3366. if (ret)
  3367. return ret;
  3368. output_event = output.file->private_data;
  3369. ret = perf_event_set_output(event, output_event);
  3370. fdput(output);
  3371. } else {
  3372. ret = perf_event_set_output(event, NULL);
  3373. }
  3374. return ret;
  3375. }
  3376. case PERF_EVENT_IOC_SET_FILTER:
  3377. return perf_event_set_filter(event, (void __user *)arg);
  3378. case PERF_EVENT_IOC_SET_BPF:
  3379. return perf_event_set_bpf_prog(event, arg);
  3380. default:
  3381. return -ENOTTY;
  3382. }
  3383. if (flags & PERF_IOC_FLAG_GROUP)
  3384. perf_event_for_each(event, func);
  3385. else
  3386. perf_event_for_each_child(event, func);
  3387. return 0;
  3388. }
  3389. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3390. {
  3391. struct perf_event *event = file->private_data;
  3392. struct perf_event_context *ctx;
  3393. long ret;
  3394. ctx = perf_event_ctx_lock(event);
  3395. ret = _perf_ioctl(event, cmd, arg);
  3396. perf_event_ctx_unlock(event, ctx);
  3397. return ret;
  3398. }
  3399. #ifdef CONFIG_COMPAT
  3400. static long perf_compat_ioctl(struct file *file, unsigned int cmd,
  3401. unsigned long arg)
  3402. {
  3403. switch (_IOC_NR(cmd)) {
  3404. case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
  3405. case _IOC_NR(PERF_EVENT_IOC_ID):
  3406. /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
  3407. if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
  3408. cmd &= ~IOCSIZE_MASK;
  3409. cmd |= sizeof(void *) << IOCSIZE_SHIFT;
  3410. }
  3411. break;
  3412. }
  3413. return perf_ioctl(file, cmd, arg);
  3414. }
  3415. #else
  3416. # define perf_compat_ioctl NULL
  3417. #endif
  3418. int perf_event_task_enable(void)
  3419. {
  3420. struct perf_event_context *ctx;
  3421. struct perf_event *event;
  3422. mutex_lock(&current->perf_event_mutex);
  3423. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3424. ctx = perf_event_ctx_lock(event);
  3425. perf_event_for_each_child(event, _perf_event_enable);
  3426. perf_event_ctx_unlock(event, ctx);
  3427. }
  3428. mutex_unlock(&current->perf_event_mutex);
  3429. return 0;
  3430. }
  3431. int perf_event_task_disable(void)
  3432. {
  3433. struct perf_event_context *ctx;
  3434. struct perf_event *event;
  3435. mutex_lock(&current->perf_event_mutex);
  3436. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  3437. ctx = perf_event_ctx_lock(event);
  3438. perf_event_for_each_child(event, _perf_event_disable);
  3439. perf_event_ctx_unlock(event, ctx);
  3440. }
  3441. mutex_unlock(&current->perf_event_mutex);
  3442. return 0;
  3443. }
  3444. static int perf_event_index(struct perf_event *event)
  3445. {
  3446. if (event->hw.state & PERF_HES_STOPPED)
  3447. return 0;
  3448. if (event->state != PERF_EVENT_STATE_ACTIVE)
  3449. return 0;
  3450. return event->pmu->event_idx(event);
  3451. }
  3452. static void calc_timer_values(struct perf_event *event,
  3453. u64 *now,
  3454. u64 *enabled,
  3455. u64 *running)
  3456. {
  3457. u64 ctx_time;
  3458. *now = perf_clock();
  3459. ctx_time = event->shadow_ctx_time + *now;
  3460. *enabled = ctx_time - event->tstamp_enabled;
  3461. *running = ctx_time - event->tstamp_running;
  3462. }
  3463. static void perf_event_init_userpage(struct perf_event *event)
  3464. {
  3465. struct perf_event_mmap_page *userpg;
  3466. struct ring_buffer *rb;
  3467. rcu_read_lock();
  3468. rb = rcu_dereference(event->rb);
  3469. if (!rb)
  3470. goto unlock;
  3471. userpg = rb->user_page;
  3472. /* Allow new userspace to detect that bit 0 is deprecated */
  3473. userpg->cap_bit0_is_deprecated = 1;
  3474. userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
  3475. userpg->data_offset = PAGE_SIZE;
  3476. userpg->data_size = perf_data_size(rb);
  3477. unlock:
  3478. rcu_read_unlock();
  3479. }
  3480. void __weak arch_perf_update_userpage(
  3481. struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
  3482. {
  3483. }
  3484. /*
  3485. * Callers need to ensure there can be no nesting of this function, otherwise
  3486. * the seqlock logic goes bad. We can not serialize this because the arch
  3487. * code calls this from NMI context.
  3488. */
  3489. void perf_event_update_userpage(struct perf_event *event)
  3490. {
  3491. struct perf_event_mmap_page *userpg;
  3492. struct ring_buffer *rb;
  3493. u64 enabled, running, now;
  3494. rcu_read_lock();
  3495. rb = rcu_dereference(event->rb);
  3496. if (!rb)
  3497. goto unlock;
  3498. /*
  3499. * compute total_time_enabled, total_time_running
  3500. * based on snapshot values taken when the event
  3501. * was last scheduled in.
  3502. *
  3503. * we cannot simply called update_context_time()
  3504. * because of locking issue as we can be called in
  3505. * NMI context
  3506. */
  3507. calc_timer_values(event, &now, &enabled, &running);
  3508. userpg = rb->user_page;
  3509. /*
  3510. * Disable preemption so as to not let the corresponding user-space
  3511. * spin too long if we get preempted.
  3512. */
  3513. preempt_disable();
  3514. ++userpg->lock;
  3515. barrier();
  3516. userpg->index = perf_event_index(event);
  3517. userpg->offset = perf_event_count(event);
  3518. if (userpg->index)
  3519. userpg->offset -= local64_read(&event->hw.prev_count);
  3520. userpg->time_enabled = enabled +
  3521. atomic64_read(&event->child_total_time_enabled);
  3522. userpg->time_running = running +
  3523. atomic64_read(&event->child_total_time_running);
  3524. arch_perf_update_userpage(event, userpg, now);
  3525. barrier();
  3526. ++userpg->lock;
  3527. preempt_enable();
  3528. unlock:
  3529. rcu_read_unlock();
  3530. }
  3531. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  3532. {
  3533. struct perf_event *event = vma->vm_file->private_data;
  3534. struct ring_buffer *rb;
  3535. int ret = VM_FAULT_SIGBUS;
  3536. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  3537. if (vmf->pgoff == 0)
  3538. ret = 0;
  3539. return ret;
  3540. }
  3541. rcu_read_lock();
  3542. rb = rcu_dereference(event->rb);
  3543. if (!rb)
  3544. goto unlock;
  3545. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  3546. goto unlock;
  3547. vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
  3548. if (!vmf->page)
  3549. goto unlock;
  3550. get_page(vmf->page);
  3551. vmf->page->mapping = vma->vm_file->f_mapping;
  3552. vmf->page->index = vmf->pgoff;
  3553. ret = 0;
  3554. unlock:
  3555. rcu_read_unlock();
  3556. return ret;
  3557. }
  3558. static void ring_buffer_attach(struct perf_event *event,
  3559. struct ring_buffer *rb)
  3560. {
  3561. struct ring_buffer *old_rb = NULL;
  3562. unsigned long flags;
  3563. if (event->rb) {
  3564. /*
  3565. * Should be impossible, we set this when removing
  3566. * event->rb_entry and wait/clear when adding event->rb_entry.
  3567. */
  3568. WARN_ON_ONCE(event->rcu_pending);
  3569. old_rb = event->rb;
  3570. spin_lock_irqsave(&old_rb->event_lock, flags);
  3571. list_del_rcu(&event->rb_entry);
  3572. spin_unlock_irqrestore(&old_rb->event_lock, flags);
  3573. event->rcu_batches = get_state_synchronize_rcu();
  3574. event->rcu_pending = 1;
  3575. }
  3576. if (rb) {
  3577. if (event->rcu_pending) {
  3578. cond_synchronize_rcu(event->rcu_batches);
  3579. event->rcu_pending = 0;
  3580. }
  3581. spin_lock_irqsave(&rb->event_lock, flags);
  3582. list_add_rcu(&event->rb_entry, &rb->event_list);
  3583. spin_unlock_irqrestore(&rb->event_lock, flags);
  3584. }
  3585. rcu_assign_pointer(event->rb, rb);
  3586. if (old_rb) {
  3587. ring_buffer_put(old_rb);
  3588. /*
  3589. * Since we detached before setting the new rb, so that we
  3590. * could attach the new rb, we could have missed a wakeup.
  3591. * Provide it now.
  3592. */
  3593. wake_up_all(&event->waitq);
  3594. }
  3595. }
  3596. static void ring_buffer_wakeup(struct perf_event *event)
  3597. {
  3598. struct ring_buffer *rb;
  3599. rcu_read_lock();
  3600. rb = rcu_dereference(event->rb);
  3601. if (rb) {
  3602. list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
  3603. wake_up_all(&event->waitq);
  3604. }
  3605. rcu_read_unlock();
  3606. }
  3607. static void rb_free_rcu(struct rcu_head *rcu_head)
  3608. {
  3609. struct ring_buffer *rb;
  3610. rb = container_of(rcu_head, struct ring_buffer, rcu_head);
  3611. rb_free(rb);
  3612. }
  3613. struct ring_buffer *ring_buffer_get(struct perf_event *event)
  3614. {
  3615. struct ring_buffer *rb;
  3616. rcu_read_lock();
  3617. rb = rcu_dereference(event->rb);
  3618. if (rb) {
  3619. if (!atomic_inc_not_zero(&rb->refcount))
  3620. rb = NULL;
  3621. }
  3622. rcu_read_unlock();
  3623. return rb;
  3624. }
  3625. void ring_buffer_put(struct ring_buffer *rb)
  3626. {
  3627. if (!atomic_dec_and_test(&rb->refcount))
  3628. return;
  3629. WARN_ON_ONCE(!list_empty(&rb->event_list));
  3630. call_rcu(&rb->rcu_head, rb_free_rcu);
  3631. }
  3632. static void perf_mmap_open(struct vm_area_struct *vma)
  3633. {
  3634. struct perf_event *event = vma->vm_file->private_data;
  3635. atomic_inc(&event->mmap_count);
  3636. atomic_inc(&event->rb->mmap_count);
  3637. if (vma->vm_pgoff)
  3638. atomic_inc(&event->rb->aux_mmap_count);
  3639. if (event->pmu->event_mapped)
  3640. event->pmu->event_mapped(event);
  3641. }
  3642. /*
  3643. * A buffer can be mmap()ed multiple times; either directly through the same
  3644. * event, or through other events by use of perf_event_set_output().
  3645. *
  3646. * In order to undo the VM accounting done by perf_mmap() we need to destroy
  3647. * the buffer here, where we still have a VM context. This means we need
  3648. * to detach all events redirecting to us.
  3649. */
  3650. static void perf_mmap_close(struct vm_area_struct *vma)
  3651. {
  3652. struct perf_event *event = vma->vm_file->private_data;
  3653. struct ring_buffer *rb = ring_buffer_get(event);
  3654. struct user_struct *mmap_user = rb->mmap_user;
  3655. int mmap_locked = rb->mmap_locked;
  3656. unsigned long size = perf_data_size(rb);
  3657. if (event->pmu->event_unmapped)
  3658. event->pmu->event_unmapped(event);
  3659. /*
  3660. * rb->aux_mmap_count will always drop before rb->mmap_count and
  3661. * event->mmap_count, so it is ok to use event->mmap_mutex to
  3662. * serialize with perf_mmap here.
  3663. */
  3664. if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
  3665. atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
  3666. atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
  3667. vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
  3668. rb_free_aux(rb);
  3669. mutex_unlock(&event->mmap_mutex);
  3670. }
  3671. atomic_dec(&rb->mmap_count);
  3672. if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
  3673. goto out_put;
  3674. ring_buffer_attach(event, NULL);
  3675. mutex_unlock(&event->mmap_mutex);
  3676. /* If there's still other mmap()s of this buffer, we're done. */
  3677. if (atomic_read(&rb->mmap_count))
  3678. goto out_put;
  3679. /*
  3680. * No other mmap()s, detach from all other events that might redirect
  3681. * into the now unreachable buffer. Somewhat complicated by the
  3682. * fact that rb::event_lock otherwise nests inside mmap_mutex.
  3683. */
  3684. again:
  3685. rcu_read_lock();
  3686. list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
  3687. if (!atomic_long_inc_not_zero(&event->refcount)) {
  3688. /*
  3689. * This event is en-route to free_event() which will
  3690. * detach it and remove it from the list.
  3691. */
  3692. continue;
  3693. }
  3694. rcu_read_unlock();
  3695. mutex_lock(&event->mmap_mutex);
  3696. /*
  3697. * Check we didn't race with perf_event_set_output() which can
  3698. * swizzle the rb from under us while we were waiting to
  3699. * acquire mmap_mutex.
  3700. *
  3701. * If we find a different rb; ignore this event, a next
  3702. * iteration will no longer find it on the list. We have to
  3703. * still restart the iteration to make sure we're not now
  3704. * iterating the wrong list.
  3705. */
  3706. if (event->rb == rb)
  3707. ring_buffer_attach(event, NULL);
  3708. mutex_unlock(&event->mmap_mutex);
  3709. put_event(event);
  3710. /*
  3711. * Restart the iteration; either we're on the wrong list or
  3712. * destroyed its integrity by doing a deletion.
  3713. */
  3714. goto again;
  3715. }
  3716. rcu_read_unlock();
  3717. /*
  3718. * It could be there's still a few 0-ref events on the list; they'll
  3719. * get cleaned up by free_event() -- they'll also still have their
  3720. * ref on the rb and will free it whenever they are done with it.
  3721. *
  3722. * Aside from that, this buffer is 'fully' detached and unmapped,
  3723. * undo the VM accounting.
  3724. */
  3725. atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
  3726. vma->vm_mm->pinned_vm -= mmap_locked;
  3727. free_uid(mmap_user);
  3728. out_put:
  3729. ring_buffer_put(rb); /* could be last */
  3730. }
  3731. static const struct vm_operations_struct perf_mmap_vmops = {
  3732. .open = perf_mmap_open,
  3733. .close = perf_mmap_close, /* non mergable */
  3734. .fault = perf_mmap_fault,
  3735. .page_mkwrite = perf_mmap_fault,
  3736. };
  3737. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  3738. {
  3739. struct perf_event *event = file->private_data;
  3740. unsigned long user_locked, user_lock_limit;
  3741. struct user_struct *user = current_user();
  3742. unsigned long locked, lock_limit;
  3743. struct ring_buffer *rb = NULL;
  3744. unsigned long vma_size;
  3745. unsigned long nr_pages;
  3746. long user_extra = 0, extra = 0;
  3747. int ret = 0, flags = 0;
  3748. /*
  3749. * Don't allow mmap() of inherited per-task counters. This would
  3750. * create a performance issue due to all children writing to the
  3751. * same rb.
  3752. */
  3753. if (event->cpu == -1 && event->attr.inherit)
  3754. return -EINVAL;
  3755. if (!(vma->vm_flags & VM_SHARED))
  3756. return -EINVAL;
  3757. vma_size = vma->vm_end - vma->vm_start;
  3758. if (vma->vm_pgoff == 0) {
  3759. nr_pages = (vma_size / PAGE_SIZE) - 1;
  3760. } else {
  3761. /*
  3762. * AUX area mapping: if rb->aux_nr_pages != 0, it's already
  3763. * mapped, all subsequent mappings should have the same size
  3764. * and offset. Must be above the normal perf buffer.
  3765. */
  3766. u64 aux_offset, aux_size;
  3767. if (!event->rb)
  3768. return -EINVAL;
  3769. nr_pages = vma_size / PAGE_SIZE;
  3770. mutex_lock(&event->mmap_mutex);
  3771. ret = -EINVAL;
  3772. rb = event->rb;
  3773. if (!rb)
  3774. goto aux_unlock;
  3775. aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
  3776. aux_size = ACCESS_ONCE(rb->user_page->aux_size);
  3777. if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
  3778. goto aux_unlock;
  3779. if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
  3780. goto aux_unlock;
  3781. /* already mapped with a different offset */
  3782. if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
  3783. goto aux_unlock;
  3784. if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
  3785. goto aux_unlock;
  3786. /* already mapped with a different size */
  3787. if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
  3788. goto aux_unlock;
  3789. if (!is_power_of_2(nr_pages))
  3790. goto aux_unlock;
  3791. if (!atomic_inc_not_zero(&rb->mmap_count))
  3792. goto aux_unlock;
  3793. if (rb_has_aux(rb)) {
  3794. atomic_inc(&rb->aux_mmap_count);
  3795. ret = 0;
  3796. goto unlock;
  3797. }
  3798. atomic_set(&rb->aux_mmap_count, 1);
  3799. user_extra = nr_pages;
  3800. goto accounting;
  3801. }
  3802. /*
  3803. * If we have rb pages ensure they're a power-of-two number, so we
  3804. * can do bitmasks instead of modulo.
  3805. */
  3806. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  3807. return -EINVAL;
  3808. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  3809. return -EINVAL;
  3810. WARN_ON_ONCE(event->ctx->parent_ctx);
  3811. again:
  3812. mutex_lock(&event->mmap_mutex);
  3813. if (event->rb) {
  3814. if (event->rb->nr_pages != nr_pages) {
  3815. ret = -EINVAL;
  3816. goto unlock;
  3817. }
  3818. if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
  3819. /*
  3820. * Raced against perf_mmap_close() through
  3821. * perf_event_set_output(). Try again, hope for better
  3822. * luck.
  3823. */
  3824. mutex_unlock(&event->mmap_mutex);
  3825. goto again;
  3826. }
  3827. goto unlock;
  3828. }
  3829. user_extra = nr_pages + 1;
  3830. accounting:
  3831. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  3832. /*
  3833. * Increase the limit linearly with more CPUs:
  3834. */
  3835. user_lock_limit *= num_online_cpus();
  3836. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  3837. if (user_locked > user_lock_limit)
  3838. extra = user_locked - user_lock_limit;
  3839. lock_limit = rlimit(RLIMIT_MEMLOCK);
  3840. lock_limit >>= PAGE_SHIFT;
  3841. locked = vma->vm_mm->pinned_vm + extra;
  3842. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  3843. !capable(CAP_IPC_LOCK)) {
  3844. ret = -EPERM;
  3845. goto unlock;
  3846. }
  3847. WARN_ON(!rb && event->rb);
  3848. if (vma->vm_flags & VM_WRITE)
  3849. flags |= RING_BUFFER_WRITABLE;
  3850. if (!rb) {
  3851. rb = rb_alloc(nr_pages,
  3852. event->attr.watermark ? event->attr.wakeup_watermark : 0,
  3853. event->cpu, flags);
  3854. if (!rb) {
  3855. ret = -ENOMEM;
  3856. goto unlock;
  3857. }
  3858. atomic_set(&rb->mmap_count, 1);
  3859. rb->mmap_user = get_current_user();
  3860. rb->mmap_locked = extra;
  3861. ring_buffer_attach(event, rb);
  3862. perf_event_init_userpage(event);
  3863. perf_event_update_userpage(event);
  3864. } else {
  3865. ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
  3866. event->attr.aux_watermark, flags);
  3867. if (!ret)
  3868. rb->aux_mmap_locked = extra;
  3869. }
  3870. unlock:
  3871. if (!ret) {
  3872. atomic_long_add(user_extra, &user->locked_vm);
  3873. vma->vm_mm->pinned_vm += extra;
  3874. atomic_inc(&event->mmap_count);
  3875. } else if (rb) {
  3876. atomic_dec(&rb->mmap_count);
  3877. }
  3878. aux_unlock:
  3879. mutex_unlock(&event->mmap_mutex);
  3880. /*
  3881. * Since pinned accounting is per vm we cannot allow fork() to copy our
  3882. * vma.
  3883. */
  3884. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
  3885. vma->vm_ops = &perf_mmap_vmops;
  3886. if (event->pmu->event_mapped)
  3887. event->pmu->event_mapped(event);
  3888. return ret;
  3889. }
  3890. static int perf_fasync(int fd, struct file *filp, int on)
  3891. {
  3892. struct inode *inode = file_inode(filp);
  3893. struct perf_event *event = filp->private_data;
  3894. int retval;
  3895. mutex_lock(&inode->i_mutex);
  3896. retval = fasync_helper(fd, filp, on, &event->fasync);
  3897. mutex_unlock(&inode->i_mutex);
  3898. if (retval < 0)
  3899. return retval;
  3900. return 0;
  3901. }
  3902. static const struct file_operations perf_fops = {
  3903. .llseek = no_llseek,
  3904. .release = perf_release,
  3905. .read = perf_read,
  3906. .poll = perf_poll,
  3907. .unlocked_ioctl = perf_ioctl,
  3908. .compat_ioctl = perf_compat_ioctl,
  3909. .mmap = perf_mmap,
  3910. .fasync = perf_fasync,
  3911. };
  3912. /*
  3913. * Perf event wakeup
  3914. *
  3915. * If there's data, ensure we set the poll() state and publish everything
  3916. * to user-space before waking everybody up.
  3917. */
  3918. void perf_event_wakeup(struct perf_event *event)
  3919. {
  3920. ring_buffer_wakeup(event);
  3921. if (event->pending_kill) {
  3922. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  3923. event->pending_kill = 0;
  3924. }
  3925. }
  3926. static void perf_pending_event(struct irq_work *entry)
  3927. {
  3928. struct perf_event *event = container_of(entry,
  3929. struct perf_event, pending);
  3930. int rctx;
  3931. rctx = perf_swevent_get_recursion_context();
  3932. /*
  3933. * If we 'fail' here, that's OK, it means recursion is already disabled
  3934. * and we won't recurse 'further'.
  3935. */
  3936. if (event->pending_disable) {
  3937. event->pending_disable = 0;
  3938. __perf_event_disable(event);
  3939. }
  3940. if (event->pending_wakeup) {
  3941. event->pending_wakeup = 0;
  3942. perf_event_wakeup(event);
  3943. }
  3944. if (rctx >= 0)
  3945. perf_swevent_put_recursion_context(rctx);
  3946. }
  3947. /*
  3948. * We assume there is only KVM supporting the callbacks.
  3949. * Later on, we might change it to a list if there is
  3950. * another virtualization implementation supporting the callbacks.
  3951. */
  3952. struct perf_guest_info_callbacks *perf_guest_cbs;
  3953. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3954. {
  3955. perf_guest_cbs = cbs;
  3956. return 0;
  3957. }
  3958. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  3959. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3960. {
  3961. perf_guest_cbs = NULL;
  3962. return 0;
  3963. }
  3964. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  3965. static void
  3966. perf_output_sample_regs(struct perf_output_handle *handle,
  3967. struct pt_regs *regs, u64 mask)
  3968. {
  3969. int bit;
  3970. for_each_set_bit(bit, (const unsigned long *) &mask,
  3971. sizeof(mask) * BITS_PER_BYTE) {
  3972. u64 val;
  3973. val = perf_reg_value(regs, bit);
  3974. perf_output_put(handle, val);
  3975. }
  3976. }
  3977. static void perf_sample_regs_user(struct perf_regs *regs_user,
  3978. struct pt_regs *regs,
  3979. struct pt_regs *regs_user_copy)
  3980. {
  3981. if (user_mode(regs)) {
  3982. regs_user->abi = perf_reg_abi(current);
  3983. regs_user->regs = regs;
  3984. } else if (current->mm) {
  3985. perf_get_regs_user(regs_user, regs, regs_user_copy);
  3986. } else {
  3987. regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
  3988. regs_user->regs = NULL;
  3989. }
  3990. }
  3991. static void perf_sample_regs_intr(struct perf_regs *regs_intr,
  3992. struct pt_regs *regs)
  3993. {
  3994. regs_intr->regs = regs;
  3995. regs_intr->abi = perf_reg_abi(current);
  3996. }
  3997. /*
  3998. * Get remaining task size from user stack pointer.
  3999. *
  4000. * It'd be better to take stack vma map and limit this more
  4001. * precisly, but there's no way to get it safely under interrupt,
  4002. * so using TASK_SIZE as limit.
  4003. */
  4004. static u64 perf_ustack_task_size(struct pt_regs *regs)
  4005. {
  4006. unsigned long addr = perf_user_stack_pointer(regs);
  4007. if (!addr || addr >= TASK_SIZE)
  4008. return 0;
  4009. return TASK_SIZE - addr;
  4010. }
  4011. static u16
  4012. perf_sample_ustack_size(u16 stack_size, u16 header_size,
  4013. struct pt_regs *regs)
  4014. {
  4015. u64 task_size;
  4016. /* No regs, no stack pointer, no dump. */
  4017. if (!regs)
  4018. return 0;
  4019. /*
  4020. * Check if we fit in with the requested stack size into the:
  4021. * - TASK_SIZE
  4022. * If we don't, we limit the size to the TASK_SIZE.
  4023. *
  4024. * - remaining sample size
  4025. * If we don't, we customize the stack size to
  4026. * fit in to the remaining sample size.
  4027. */
  4028. task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
  4029. stack_size = min(stack_size, (u16) task_size);
  4030. /* Current header size plus static size and dynamic size. */
  4031. header_size += 2 * sizeof(u64);
  4032. /* Do we fit in with the current stack dump size? */
  4033. if ((u16) (header_size + stack_size) < header_size) {
  4034. /*
  4035. * If we overflow the maximum size for the sample,
  4036. * we customize the stack dump size to fit in.
  4037. */
  4038. stack_size = USHRT_MAX - header_size - sizeof(u64);
  4039. stack_size = round_up(stack_size, sizeof(u64));
  4040. }
  4041. return stack_size;
  4042. }
  4043. static void
  4044. perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
  4045. struct pt_regs *regs)
  4046. {
  4047. /* Case of a kernel thread, nothing to dump */
  4048. if (!regs) {
  4049. u64 size = 0;
  4050. perf_output_put(handle, size);
  4051. } else {
  4052. unsigned long sp;
  4053. unsigned int rem;
  4054. u64 dyn_size;
  4055. /*
  4056. * We dump:
  4057. * static size
  4058. * - the size requested by user or the best one we can fit
  4059. * in to the sample max size
  4060. * data
  4061. * - user stack dump data
  4062. * dynamic size
  4063. * - the actual dumped size
  4064. */
  4065. /* Static size. */
  4066. perf_output_put(handle, dump_size);
  4067. /* Data. */
  4068. sp = perf_user_stack_pointer(regs);
  4069. rem = __output_copy_user(handle, (void *) sp, dump_size);
  4070. dyn_size = dump_size - rem;
  4071. perf_output_skip(handle, rem);
  4072. /* Dynamic size. */
  4073. perf_output_put(handle, dyn_size);
  4074. }
  4075. }
  4076. static void __perf_event_header__init_id(struct perf_event_header *header,
  4077. struct perf_sample_data *data,
  4078. struct perf_event *event)
  4079. {
  4080. u64 sample_type = event->attr.sample_type;
  4081. data->type = sample_type;
  4082. header->size += event->id_header_size;
  4083. if (sample_type & PERF_SAMPLE_TID) {
  4084. /* namespace issues */
  4085. data->tid_entry.pid = perf_event_pid(event, current);
  4086. data->tid_entry.tid = perf_event_tid(event, current);
  4087. }
  4088. if (sample_type & PERF_SAMPLE_TIME)
  4089. data->time = perf_event_clock(event);
  4090. if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
  4091. data->id = primary_event_id(event);
  4092. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4093. data->stream_id = event->id;
  4094. if (sample_type & PERF_SAMPLE_CPU) {
  4095. data->cpu_entry.cpu = raw_smp_processor_id();
  4096. data->cpu_entry.reserved = 0;
  4097. }
  4098. }
  4099. void perf_event_header__init_id(struct perf_event_header *header,
  4100. struct perf_sample_data *data,
  4101. struct perf_event *event)
  4102. {
  4103. if (event->attr.sample_id_all)
  4104. __perf_event_header__init_id(header, data, event);
  4105. }
  4106. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  4107. struct perf_sample_data *data)
  4108. {
  4109. u64 sample_type = data->type;
  4110. if (sample_type & PERF_SAMPLE_TID)
  4111. perf_output_put(handle, data->tid_entry);
  4112. if (sample_type & PERF_SAMPLE_TIME)
  4113. perf_output_put(handle, data->time);
  4114. if (sample_type & PERF_SAMPLE_ID)
  4115. perf_output_put(handle, data->id);
  4116. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4117. perf_output_put(handle, data->stream_id);
  4118. if (sample_type & PERF_SAMPLE_CPU)
  4119. perf_output_put(handle, data->cpu_entry);
  4120. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  4121. perf_output_put(handle, data->id);
  4122. }
  4123. void perf_event__output_id_sample(struct perf_event *event,
  4124. struct perf_output_handle *handle,
  4125. struct perf_sample_data *sample)
  4126. {
  4127. if (event->attr.sample_id_all)
  4128. __perf_event__output_id_sample(handle, sample);
  4129. }
  4130. static void perf_output_read_one(struct perf_output_handle *handle,
  4131. struct perf_event *event,
  4132. u64 enabled, u64 running)
  4133. {
  4134. u64 read_format = event->attr.read_format;
  4135. u64 values[4];
  4136. int n = 0;
  4137. values[n++] = perf_event_count(event);
  4138. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  4139. values[n++] = enabled +
  4140. atomic64_read(&event->child_total_time_enabled);
  4141. }
  4142. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  4143. values[n++] = running +
  4144. atomic64_read(&event->child_total_time_running);
  4145. }
  4146. if (read_format & PERF_FORMAT_ID)
  4147. values[n++] = primary_event_id(event);
  4148. __output_copy(handle, values, n * sizeof(u64));
  4149. }
  4150. /*
  4151. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  4152. */
  4153. static void perf_output_read_group(struct perf_output_handle *handle,
  4154. struct perf_event *event,
  4155. u64 enabled, u64 running)
  4156. {
  4157. struct perf_event *leader = event->group_leader, *sub;
  4158. u64 read_format = event->attr.read_format;
  4159. u64 values[5];
  4160. int n = 0;
  4161. values[n++] = 1 + leader->nr_siblings;
  4162. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  4163. values[n++] = enabled;
  4164. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  4165. values[n++] = running;
  4166. if (leader != event)
  4167. leader->pmu->read(leader);
  4168. values[n++] = perf_event_count(leader);
  4169. if (read_format & PERF_FORMAT_ID)
  4170. values[n++] = primary_event_id(leader);
  4171. __output_copy(handle, values, n * sizeof(u64));
  4172. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  4173. n = 0;
  4174. if ((sub != event) &&
  4175. (sub->state == PERF_EVENT_STATE_ACTIVE))
  4176. sub->pmu->read(sub);
  4177. values[n++] = perf_event_count(sub);
  4178. if (read_format & PERF_FORMAT_ID)
  4179. values[n++] = primary_event_id(sub);
  4180. __output_copy(handle, values, n * sizeof(u64));
  4181. }
  4182. }
  4183. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  4184. PERF_FORMAT_TOTAL_TIME_RUNNING)
  4185. static void perf_output_read(struct perf_output_handle *handle,
  4186. struct perf_event *event)
  4187. {
  4188. u64 enabled = 0, running = 0, now;
  4189. u64 read_format = event->attr.read_format;
  4190. /*
  4191. * compute total_time_enabled, total_time_running
  4192. * based on snapshot values taken when the event
  4193. * was last scheduled in.
  4194. *
  4195. * we cannot simply called update_context_time()
  4196. * because of locking issue as we are called in
  4197. * NMI context
  4198. */
  4199. if (read_format & PERF_FORMAT_TOTAL_TIMES)
  4200. calc_timer_values(event, &now, &enabled, &running);
  4201. if (event->attr.read_format & PERF_FORMAT_GROUP)
  4202. perf_output_read_group(handle, event, enabled, running);
  4203. else
  4204. perf_output_read_one(handle, event, enabled, running);
  4205. }
  4206. void perf_output_sample(struct perf_output_handle *handle,
  4207. struct perf_event_header *header,
  4208. struct perf_sample_data *data,
  4209. struct perf_event *event)
  4210. {
  4211. u64 sample_type = data->type;
  4212. perf_output_put(handle, *header);
  4213. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  4214. perf_output_put(handle, data->id);
  4215. if (sample_type & PERF_SAMPLE_IP)
  4216. perf_output_put(handle, data->ip);
  4217. if (sample_type & PERF_SAMPLE_TID)
  4218. perf_output_put(handle, data->tid_entry);
  4219. if (sample_type & PERF_SAMPLE_TIME)
  4220. perf_output_put(handle, data->time);
  4221. if (sample_type & PERF_SAMPLE_ADDR)
  4222. perf_output_put(handle, data->addr);
  4223. if (sample_type & PERF_SAMPLE_ID)
  4224. perf_output_put(handle, data->id);
  4225. if (sample_type & PERF_SAMPLE_STREAM_ID)
  4226. perf_output_put(handle, data->stream_id);
  4227. if (sample_type & PERF_SAMPLE_CPU)
  4228. perf_output_put(handle, data->cpu_entry);
  4229. if (sample_type & PERF_SAMPLE_PERIOD)
  4230. perf_output_put(handle, data->period);
  4231. if (sample_type & PERF_SAMPLE_READ)
  4232. perf_output_read(handle, event);
  4233. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  4234. if (data->callchain) {
  4235. int size = 1;
  4236. if (data->callchain)
  4237. size += data->callchain->nr;
  4238. size *= sizeof(u64);
  4239. __output_copy(handle, data->callchain, size);
  4240. } else {
  4241. u64 nr = 0;
  4242. perf_output_put(handle, nr);
  4243. }
  4244. }
  4245. if (sample_type & PERF_SAMPLE_RAW) {
  4246. if (data->raw) {
  4247. perf_output_put(handle, data->raw->size);
  4248. __output_copy(handle, data->raw->data,
  4249. data->raw->size);
  4250. } else {
  4251. struct {
  4252. u32 size;
  4253. u32 data;
  4254. } raw = {
  4255. .size = sizeof(u32),
  4256. .data = 0,
  4257. };
  4258. perf_output_put(handle, raw);
  4259. }
  4260. }
  4261. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  4262. if (data->br_stack) {
  4263. size_t size;
  4264. size = data->br_stack->nr
  4265. * sizeof(struct perf_branch_entry);
  4266. perf_output_put(handle, data->br_stack->nr);
  4267. perf_output_copy(handle, data->br_stack->entries, size);
  4268. } else {
  4269. /*
  4270. * we always store at least the value of nr
  4271. */
  4272. u64 nr = 0;
  4273. perf_output_put(handle, nr);
  4274. }
  4275. }
  4276. if (sample_type & PERF_SAMPLE_REGS_USER) {
  4277. u64 abi = data->regs_user.abi;
  4278. /*
  4279. * If there are no regs to dump, notice it through
  4280. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  4281. */
  4282. perf_output_put(handle, abi);
  4283. if (abi) {
  4284. u64 mask = event->attr.sample_regs_user;
  4285. perf_output_sample_regs(handle,
  4286. data->regs_user.regs,
  4287. mask);
  4288. }
  4289. }
  4290. if (sample_type & PERF_SAMPLE_STACK_USER) {
  4291. perf_output_sample_ustack(handle,
  4292. data->stack_user_size,
  4293. data->regs_user.regs);
  4294. }
  4295. if (sample_type & PERF_SAMPLE_WEIGHT)
  4296. perf_output_put(handle, data->weight);
  4297. if (sample_type & PERF_SAMPLE_DATA_SRC)
  4298. perf_output_put(handle, data->data_src.val);
  4299. if (sample_type & PERF_SAMPLE_TRANSACTION)
  4300. perf_output_put(handle, data->txn);
  4301. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  4302. u64 abi = data->regs_intr.abi;
  4303. /*
  4304. * If there are no regs to dump, notice it through
  4305. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  4306. */
  4307. perf_output_put(handle, abi);
  4308. if (abi) {
  4309. u64 mask = event->attr.sample_regs_intr;
  4310. perf_output_sample_regs(handle,
  4311. data->regs_intr.regs,
  4312. mask);
  4313. }
  4314. }
  4315. if (!event->attr.watermark) {
  4316. int wakeup_events = event->attr.wakeup_events;
  4317. if (wakeup_events) {
  4318. struct ring_buffer *rb = handle->rb;
  4319. int events = local_inc_return(&rb->events);
  4320. if (events >= wakeup_events) {
  4321. local_sub(wakeup_events, &rb->events);
  4322. local_inc(&rb->wakeup);
  4323. }
  4324. }
  4325. }
  4326. }
  4327. void perf_prepare_sample(struct perf_event_header *header,
  4328. struct perf_sample_data *data,
  4329. struct perf_event *event,
  4330. struct pt_regs *regs)
  4331. {
  4332. u64 sample_type = event->attr.sample_type;
  4333. header->type = PERF_RECORD_SAMPLE;
  4334. header->size = sizeof(*header) + event->header_size;
  4335. header->misc = 0;
  4336. header->misc |= perf_misc_flags(regs);
  4337. __perf_event_header__init_id(header, data, event);
  4338. if (sample_type & PERF_SAMPLE_IP)
  4339. data->ip = perf_instruction_pointer(regs);
  4340. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  4341. int size = 1;
  4342. data->callchain = perf_callchain(event, regs);
  4343. if (data->callchain)
  4344. size += data->callchain->nr;
  4345. header->size += size * sizeof(u64);
  4346. }
  4347. if (sample_type & PERF_SAMPLE_RAW) {
  4348. int size = sizeof(u32);
  4349. if (data->raw)
  4350. size += data->raw->size;
  4351. else
  4352. size += sizeof(u32);
  4353. WARN_ON_ONCE(size & (sizeof(u64)-1));
  4354. header->size += size;
  4355. }
  4356. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  4357. int size = sizeof(u64); /* nr */
  4358. if (data->br_stack) {
  4359. size += data->br_stack->nr
  4360. * sizeof(struct perf_branch_entry);
  4361. }
  4362. header->size += size;
  4363. }
  4364. if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
  4365. perf_sample_regs_user(&data->regs_user, regs,
  4366. &data->regs_user_copy);
  4367. if (sample_type & PERF_SAMPLE_REGS_USER) {
  4368. /* regs dump ABI info */
  4369. int size = sizeof(u64);
  4370. if (data->regs_user.regs) {
  4371. u64 mask = event->attr.sample_regs_user;
  4372. size += hweight64(mask) * sizeof(u64);
  4373. }
  4374. header->size += size;
  4375. }
  4376. if (sample_type & PERF_SAMPLE_STACK_USER) {
  4377. /*
  4378. * Either we need PERF_SAMPLE_STACK_USER bit to be allways
  4379. * processed as the last one or have additional check added
  4380. * in case new sample type is added, because we could eat
  4381. * up the rest of the sample size.
  4382. */
  4383. u16 stack_size = event->attr.sample_stack_user;
  4384. u16 size = sizeof(u64);
  4385. stack_size = perf_sample_ustack_size(stack_size, header->size,
  4386. data->regs_user.regs);
  4387. /*
  4388. * If there is something to dump, add space for the dump
  4389. * itself and for the field that tells the dynamic size,
  4390. * which is how many have been actually dumped.
  4391. */
  4392. if (stack_size)
  4393. size += sizeof(u64) + stack_size;
  4394. data->stack_user_size = stack_size;
  4395. header->size += size;
  4396. }
  4397. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  4398. /* regs dump ABI info */
  4399. int size = sizeof(u64);
  4400. perf_sample_regs_intr(&data->regs_intr, regs);
  4401. if (data->regs_intr.regs) {
  4402. u64 mask = event->attr.sample_regs_intr;
  4403. size += hweight64(mask) * sizeof(u64);
  4404. }
  4405. header->size += size;
  4406. }
  4407. }
  4408. void perf_event_output(struct perf_event *event,
  4409. struct perf_sample_data *data,
  4410. struct pt_regs *regs)
  4411. {
  4412. struct perf_output_handle handle;
  4413. struct perf_event_header header;
  4414. /* protect the callchain buffers */
  4415. rcu_read_lock();
  4416. perf_prepare_sample(&header, data, event, regs);
  4417. if (perf_output_begin(&handle, event, header.size))
  4418. goto exit;
  4419. perf_output_sample(&handle, &header, data, event);
  4420. perf_output_end(&handle);
  4421. exit:
  4422. rcu_read_unlock();
  4423. }
  4424. /*
  4425. * read event_id
  4426. */
  4427. struct perf_read_event {
  4428. struct perf_event_header header;
  4429. u32 pid;
  4430. u32 tid;
  4431. };
  4432. static void
  4433. perf_event_read_event(struct perf_event *event,
  4434. struct task_struct *task)
  4435. {
  4436. struct perf_output_handle handle;
  4437. struct perf_sample_data sample;
  4438. struct perf_read_event read_event = {
  4439. .header = {
  4440. .type = PERF_RECORD_READ,
  4441. .misc = 0,
  4442. .size = sizeof(read_event) + event->read_size,
  4443. },
  4444. .pid = perf_event_pid(event, task),
  4445. .tid = perf_event_tid(event, task),
  4446. };
  4447. int ret;
  4448. perf_event_header__init_id(&read_event.header, &sample, event);
  4449. ret = perf_output_begin(&handle, event, read_event.header.size);
  4450. if (ret)
  4451. return;
  4452. perf_output_put(&handle, read_event);
  4453. perf_output_read(&handle, event);
  4454. perf_event__output_id_sample(event, &handle, &sample);
  4455. perf_output_end(&handle);
  4456. }
  4457. typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
  4458. static void
  4459. perf_event_aux_ctx(struct perf_event_context *ctx,
  4460. perf_event_aux_output_cb output,
  4461. void *data)
  4462. {
  4463. struct perf_event *event;
  4464. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  4465. if (event->state < PERF_EVENT_STATE_INACTIVE)
  4466. continue;
  4467. if (!event_filter_match(event))
  4468. continue;
  4469. output(event, data);
  4470. }
  4471. }
  4472. static void
  4473. perf_event_aux(perf_event_aux_output_cb output, void *data,
  4474. struct perf_event_context *task_ctx)
  4475. {
  4476. struct perf_cpu_context *cpuctx;
  4477. struct perf_event_context *ctx;
  4478. struct pmu *pmu;
  4479. int ctxn;
  4480. rcu_read_lock();
  4481. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4482. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  4483. if (cpuctx->unique_pmu != pmu)
  4484. goto next;
  4485. perf_event_aux_ctx(&cpuctx->ctx, output, data);
  4486. if (task_ctx)
  4487. goto next;
  4488. ctxn = pmu->task_ctx_nr;
  4489. if (ctxn < 0)
  4490. goto next;
  4491. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  4492. if (ctx)
  4493. perf_event_aux_ctx(ctx, output, data);
  4494. next:
  4495. put_cpu_ptr(pmu->pmu_cpu_context);
  4496. }
  4497. if (task_ctx) {
  4498. preempt_disable();
  4499. perf_event_aux_ctx(task_ctx, output, data);
  4500. preempt_enable();
  4501. }
  4502. rcu_read_unlock();
  4503. }
  4504. /*
  4505. * task tracking -- fork/exit
  4506. *
  4507. * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
  4508. */
  4509. struct perf_task_event {
  4510. struct task_struct *task;
  4511. struct perf_event_context *task_ctx;
  4512. struct {
  4513. struct perf_event_header header;
  4514. u32 pid;
  4515. u32 ppid;
  4516. u32 tid;
  4517. u32 ptid;
  4518. u64 time;
  4519. } event_id;
  4520. };
  4521. static int perf_event_task_match(struct perf_event *event)
  4522. {
  4523. return event->attr.comm || event->attr.mmap ||
  4524. event->attr.mmap2 || event->attr.mmap_data ||
  4525. event->attr.task;
  4526. }
  4527. static void perf_event_task_output(struct perf_event *event,
  4528. void *data)
  4529. {
  4530. struct perf_task_event *task_event = data;
  4531. struct perf_output_handle handle;
  4532. struct perf_sample_data sample;
  4533. struct task_struct *task = task_event->task;
  4534. int ret, size = task_event->event_id.header.size;
  4535. if (!perf_event_task_match(event))
  4536. return;
  4537. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  4538. ret = perf_output_begin(&handle, event,
  4539. task_event->event_id.header.size);
  4540. if (ret)
  4541. goto out;
  4542. task_event->event_id.pid = perf_event_pid(event, task);
  4543. task_event->event_id.ppid = perf_event_pid(event, current);
  4544. task_event->event_id.tid = perf_event_tid(event, task);
  4545. task_event->event_id.ptid = perf_event_tid(event, current);
  4546. task_event->event_id.time = perf_event_clock(event);
  4547. perf_output_put(&handle, task_event->event_id);
  4548. perf_event__output_id_sample(event, &handle, &sample);
  4549. perf_output_end(&handle);
  4550. out:
  4551. task_event->event_id.header.size = size;
  4552. }
  4553. static void perf_event_task(struct task_struct *task,
  4554. struct perf_event_context *task_ctx,
  4555. int new)
  4556. {
  4557. struct perf_task_event task_event;
  4558. if (!atomic_read(&nr_comm_events) &&
  4559. !atomic_read(&nr_mmap_events) &&
  4560. !atomic_read(&nr_task_events))
  4561. return;
  4562. task_event = (struct perf_task_event){
  4563. .task = task,
  4564. .task_ctx = task_ctx,
  4565. .event_id = {
  4566. .header = {
  4567. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  4568. .misc = 0,
  4569. .size = sizeof(task_event.event_id),
  4570. },
  4571. /* .pid */
  4572. /* .ppid */
  4573. /* .tid */
  4574. /* .ptid */
  4575. /* .time */
  4576. },
  4577. };
  4578. perf_event_aux(perf_event_task_output,
  4579. &task_event,
  4580. task_ctx);
  4581. }
  4582. void perf_event_fork(struct task_struct *task)
  4583. {
  4584. perf_event_task(task, NULL, 1);
  4585. }
  4586. /*
  4587. * comm tracking
  4588. */
  4589. struct perf_comm_event {
  4590. struct task_struct *task;
  4591. char *comm;
  4592. int comm_size;
  4593. struct {
  4594. struct perf_event_header header;
  4595. u32 pid;
  4596. u32 tid;
  4597. } event_id;
  4598. };
  4599. static int perf_event_comm_match(struct perf_event *event)
  4600. {
  4601. return event->attr.comm;
  4602. }
  4603. static void perf_event_comm_output(struct perf_event *event,
  4604. void *data)
  4605. {
  4606. struct perf_comm_event *comm_event = data;
  4607. struct perf_output_handle handle;
  4608. struct perf_sample_data sample;
  4609. int size = comm_event->event_id.header.size;
  4610. int ret;
  4611. if (!perf_event_comm_match(event))
  4612. return;
  4613. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  4614. ret = perf_output_begin(&handle, event,
  4615. comm_event->event_id.header.size);
  4616. if (ret)
  4617. goto out;
  4618. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  4619. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  4620. perf_output_put(&handle, comm_event->event_id);
  4621. __output_copy(&handle, comm_event->comm,
  4622. comm_event->comm_size);
  4623. perf_event__output_id_sample(event, &handle, &sample);
  4624. perf_output_end(&handle);
  4625. out:
  4626. comm_event->event_id.header.size = size;
  4627. }
  4628. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  4629. {
  4630. char comm[TASK_COMM_LEN];
  4631. unsigned int size;
  4632. memset(comm, 0, sizeof(comm));
  4633. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  4634. size = ALIGN(strlen(comm)+1, sizeof(u64));
  4635. comm_event->comm = comm;
  4636. comm_event->comm_size = size;
  4637. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  4638. perf_event_aux(perf_event_comm_output,
  4639. comm_event,
  4640. NULL);
  4641. }
  4642. void perf_event_comm(struct task_struct *task, bool exec)
  4643. {
  4644. struct perf_comm_event comm_event;
  4645. if (!atomic_read(&nr_comm_events))
  4646. return;
  4647. comm_event = (struct perf_comm_event){
  4648. .task = task,
  4649. /* .comm */
  4650. /* .comm_size */
  4651. .event_id = {
  4652. .header = {
  4653. .type = PERF_RECORD_COMM,
  4654. .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
  4655. /* .size */
  4656. },
  4657. /* .pid */
  4658. /* .tid */
  4659. },
  4660. };
  4661. perf_event_comm_event(&comm_event);
  4662. }
  4663. /*
  4664. * mmap tracking
  4665. */
  4666. struct perf_mmap_event {
  4667. struct vm_area_struct *vma;
  4668. const char *file_name;
  4669. int file_size;
  4670. int maj, min;
  4671. u64 ino;
  4672. u64 ino_generation;
  4673. u32 prot, flags;
  4674. struct {
  4675. struct perf_event_header header;
  4676. u32 pid;
  4677. u32 tid;
  4678. u64 start;
  4679. u64 len;
  4680. u64 pgoff;
  4681. } event_id;
  4682. };
  4683. static int perf_event_mmap_match(struct perf_event *event,
  4684. void *data)
  4685. {
  4686. struct perf_mmap_event *mmap_event = data;
  4687. struct vm_area_struct *vma = mmap_event->vma;
  4688. int executable = vma->vm_flags & VM_EXEC;
  4689. return (!executable && event->attr.mmap_data) ||
  4690. (executable && (event->attr.mmap || event->attr.mmap2));
  4691. }
  4692. static void perf_event_mmap_output(struct perf_event *event,
  4693. void *data)
  4694. {
  4695. struct perf_mmap_event *mmap_event = data;
  4696. struct perf_output_handle handle;
  4697. struct perf_sample_data sample;
  4698. int size = mmap_event->event_id.header.size;
  4699. int ret;
  4700. if (!perf_event_mmap_match(event, data))
  4701. return;
  4702. if (event->attr.mmap2) {
  4703. mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
  4704. mmap_event->event_id.header.size += sizeof(mmap_event->maj);
  4705. mmap_event->event_id.header.size += sizeof(mmap_event->min);
  4706. mmap_event->event_id.header.size += sizeof(mmap_event->ino);
  4707. mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
  4708. mmap_event->event_id.header.size += sizeof(mmap_event->prot);
  4709. mmap_event->event_id.header.size += sizeof(mmap_event->flags);
  4710. }
  4711. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  4712. ret = perf_output_begin(&handle, event,
  4713. mmap_event->event_id.header.size);
  4714. if (ret)
  4715. goto out;
  4716. mmap_event->event_id.pid = perf_event_pid(event, current);
  4717. mmap_event->event_id.tid = perf_event_tid(event, current);
  4718. perf_output_put(&handle, mmap_event->event_id);
  4719. if (event->attr.mmap2) {
  4720. perf_output_put(&handle, mmap_event->maj);
  4721. perf_output_put(&handle, mmap_event->min);
  4722. perf_output_put(&handle, mmap_event->ino);
  4723. perf_output_put(&handle, mmap_event->ino_generation);
  4724. perf_output_put(&handle, mmap_event->prot);
  4725. perf_output_put(&handle, mmap_event->flags);
  4726. }
  4727. __output_copy(&handle, mmap_event->file_name,
  4728. mmap_event->file_size);
  4729. perf_event__output_id_sample(event, &handle, &sample);
  4730. perf_output_end(&handle);
  4731. out:
  4732. mmap_event->event_id.header.size = size;
  4733. }
  4734. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  4735. {
  4736. struct vm_area_struct *vma = mmap_event->vma;
  4737. struct file *file = vma->vm_file;
  4738. int maj = 0, min = 0;
  4739. u64 ino = 0, gen = 0;
  4740. u32 prot = 0, flags = 0;
  4741. unsigned int size;
  4742. char tmp[16];
  4743. char *buf = NULL;
  4744. char *name;
  4745. if (file) {
  4746. struct inode *inode;
  4747. dev_t dev;
  4748. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  4749. if (!buf) {
  4750. name = "//enomem";
  4751. goto cpy_name;
  4752. }
  4753. /*
  4754. * d_path() works from the end of the rb backwards, so we
  4755. * need to add enough zero bytes after the string to handle
  4756. * the 64bit alignment we do later.
  4757. */
  4758. name = file_path(file, buf, PATH_MAX - sizeof(u64));
  4759. if (IS_ERR(name)) {
  4760. name = "//toolong";
  4761. goto cpy_name;
  4762. }
  4763. inode = file_inode(vma->vm_file);
  4764. dev = inode->i_sb->s_dev;
  4765. ino = inode->i_ino;
  4766. gen = inode->i_generation;
  4767. maj = MAJOR(dev);
  4768. min = MINOR(dev);
  4769. if (vma->vm_flags & VM_READ)
  4770. prot |= PROT_READ;
  4771. if (vma->vm_flags & VM_WRITE)
  4772. prot |= PROT_WRITE;
  4773. if (vma->vm_flags & VM_EXEC)
  4774. prot |= PROT_EXEC;
  4775. if (vma->vm_flags & VM_MAYSHARE)
  4776. flags = MAP_SHARED;
  4777. else
  4778. flags = MAP_PRIVATE;
  4779. if (vma->vm_flags & VM_DENYWRITE)
  4780. flags |= MAP_DENYWRITE;
  4781. if (vma->vm_flags & VM_MAYEXEC)
  4782. flags |= MAP_EXECUTABLE;
  4783. if (vma->vm_flags & VM_LOCKED)
  4784. flags |= MAP_LOCKED;
  4785. if (vma->vm_flags & VM_HUGETLB)
  4786. flags |= MAP_HUGETLB;
  4787. goto got_name;
  4788. } else {
  4789. if (vma->vm_ops && vma->vm_ops->name) {
  4790. name = (char *) vma->vm_ops->name(vma);
  4791. if (name)
  4792. goto cpy_name;
  4793. }
  4794. name = (char *)arch_vma_name(vma);
  4795. if (name)
  4796. goto cpy_name;
  4797. if (vma->vm_start <= vma->vm_mm->start_brk &&
  4798. vma->vm_end >= vma->vm_mm->brk) {
  4799. name = "[heap]";
  4800. goto cpy_name;
  4801. }
  4802. if (vma->vm_start <= vma->vm_mm->start_stack &&
  4803. vma->vm_end >= vma->vm_mm->start_stack) {
  4804. name = "[stack]";
  4805. goto cpy_name;
  4806. }
  4807. name = "//anon";
  4808. goto cpy_name;
  4809. }
  4810. cpy_name:
  4811. strlcpy(tmp, name, sizeof(tmp));
  4812. name = tmp;
  4813. got_name:
  4814. /*
  4815. * Since our buffer works in 8 byte units we need to align our string
  4816. * size to a multiple of 8. However, we must guarantee the tail end is
  4817. * zero'd out to avoid leaking random bits to userspace.
  4818. */
  4819. size = strlen(name)+1;
  4820. while (!IS_ALIGNED(size, sizeof(u64)))
  4821. name[size++] = '\0';
  4822. mmap_event->file_name = name;
  4823. mmap_event->file_size = size;
  4824. mmap_event->maj = maj;
  4825. mmap_event->min = min;
  4826. mmap_event->ino = ino;
  4827. mmap_event->ino_generation = gen;
  4828. mmap_event->prot = prot;
  4829. mmap_event->flags = flags;
  4830. if (!(vma->vm_flags & VM_EXEC))
  4831. mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  4832. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  4833. perf_event_aux(perf_event_mmap_output,
  4834. mmap_event,
  4835. NULL);
  4836. kfree(buf);
  4837. }
  4838. void perf_event_mmap(struct vm_area_struct *vma)
  4839. {
  4840. struct perf_mmap_event mmap_event;
  4841. if (!atomic_read(&nr_mmap_events))
  4842. return;
  4843. mmap_event = (struct perf_mmap_event){
  4844. .vma = vma,
  4845. /* .file_name */
  4846. /* .file_size */
  4847. .event_id = {
  4848. .header = {
  4849. .type = PERF_RECORD_MMAP,
  4850. .misc = PERF_RECORD_MISC_USER,
  4851. /* .size */
  4852. },
  4853. /* .pid */
  4854. /* .tid */
  4855. .start = vma->vm_start,
  4856. .len = vma->vm_end - vma->vm_start,
  4857. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  4858. },
  4859. /* .maj (attr_mmap2 only) */
  4860. /* .min (attr_mmap2 only) */
  4861. /* .ino (attr_mmap2 only) */
  4862. /* .ino_generation (attr_mmap2 only) */
  4863. /* .prot (attr_mmap2 only) */
  4864. /* .flags (attr_mmap2 only) */
  4865. };
  4866. perf_event_mmap_event(&mmap_event);
  4867. }
  4868. void perf_event_aux_event(struct perf_event *event, unsigned long head,
  4869. unsigned long size, u64 flags)
  4870. {
  4871. struct perf_output_handle handle;
  4872. struct perf_sample_data sample;
  4873. struct perf_aux_event {
  4874. struct perf_event_header header;
  4875. u64 offset;
  4876. u64 size;
  4877. u64 flags;
  4878. } rec = {
  4879. .header = {
  4880. .type = PERF_RECORD_AUX,
  4881. .misc = 0,
  4882. .size = sizeof(rec),
  4883. },
  4884. .offset = head,
  4885. .size = size,
  4886. .flags = flags,
  4887. };
  4888. int ret;
  4889. perf_event_header__init_id(&rec.header, &sample, event);
  4890. ret = perf_output_begin(&handle, event, rec.header.size);
  4891. if (ret)
  4892. return;
  4893. perf_output_put(&handle, rec);
  4894. perf_event__output_id_sample(event, &handle, &sample);
  4895. perf_output_end(&handle);
  4896. }
  4897. /*
  4898. * Lost/dropped samples logging
  4899. */
  4900. void perf_log_lost_samples(struct perf_event *event, u64 lost)
  4901. {
  4902. struct perf_output_handle handle;
  4903. struct perf_sample_data sample;
  4904. int ret;
  4905. struct {
  4906. struct perf_event_header header;
  4907. u64 lost;
  4908. } lost_samples_event = {
  4909. .header = {
  4910. .type = PERF_RECORD_LOST_SAMPLES,
  4911. .misc = 0,
  4912. .size = sizeof(lost_samples_event),
  4913. },
  4914. .lost = lost,
  4915. };
  4916. perf_event_header__init_id(&lost_samples_event.header, &sample, event);
  4917. ret = perf_output_begin(&handle, event,
  4918. lost_samples_event.header.size);
  4919. if (ret)
  4920. return;
  4921. perf_output_put(&handle, lost_samples_event);
  4922. perf_event__output_id_sample(event, &handle, &sample);
  4923. perf_output_end(&handle);
  4924. }
  4925. /*
  4926. * IRQ throttle logging
  4927. */
  4928. static void perf_log_throttle(struct perf_event *event, int enable)
  4929. {
  4930. struct perf_output_handle handle;
  4931. struct perf_sample_data sample;
  4932. int ret;
  4933. struct {
  4934. struct perf_event_header header;
  4935. u64 time;
  4936. u64 id;
  4937. u64 stream_id;
  4938. } throttle_event = {
  4939. .header = {
  4940. .type = PERF_RECORD_THROTTLE,
  4941. .misc = 0,
  4942. .size = sizeof(throttle_event),
  4943. },
  4944. .time = perf_event_clock(event),
  4945. .id = primary_event_id(event),
  4946. .stream_id = event->id,
  4947. };
  4948. if (enable)
  4949. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  4950. perf_event_header__init_id(&throttle_event.header, &sample, event);
  4951. ret = perf_output_begin(&handle, event,
  4952. throttle_event.header.size);
  4953. if (ret)
  4954. return;
  4955. perf_output_put(&handle, throttle_event);
  4956. perf_event__output_id_sample(event, &handle, &sample);
  4957. perf_output_end(&handle);
  4958. }
  4959. static void perf_log_itrace_start(struct perf_event *event)
  4960. {
  4961. struct perf_output_handle handle;
  4962. struct perf_sample_data sample;
  4963. struct perf_aux_event {
  4964. struct perf_event_header header;
  4965. u32 pid;
  4966. u32 tid;
  4967. } rec;
  4968. int ret;
  4969. if (event->parent)
  4970. event = event->parent;
  4971. if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
  4972. event->hw.itrace_started)
  4973. return;
  4974. event->hw.itrace_started = 1;
  4975. rec.header.type = PERF_RECORD_ITRACE_START;
  4976. rec.header.misc = 0;
  4977. rec.header.size = sizeof(rec);
  4978. rec.pid = perf_event_pid(event, current);
  4979. rec.tid = perf_event_tid(event, current);
  4980. perf_event_header__init_id(&rec.header, &sample, event);
  4981. ret = perf_output_begin(&handle, event, rec.header.size);
  4982. if (ret)
  4983. return;
  4984. perf_output_put(&handle, rec);
  4985. perf_event__output_id_sample(event, &handle, &sample);
  4986. perf_output_end(&handle);
  4987. }
  4988. /*
  4989. * Generic event overflow handling, sampling.
  4990. */
  4991. static int __perf_event_overflow(struct perf_event *event,
  4992. int throttle, struct perf_sample_data *data,
  4993. struct pt_regs *regs)
  4994. {
  4995. int events = atomic_read(&event->event_limit);
  4996. struct hw_perf_event *hwc = &event->hw;
  4997. u64 seq;
  4998. int ret = 0;
  4999. /*
  5000. * Non-sampling counters might still use the PMI to fold short
  5001. * hardware counters, ignore those.
  5002. */
  5003. if (unlikely(!is_sampling_event(event)))
  5004. return 0;
  5005. seq = __this_cpu_read(perf_throttled_seq);
  5006. if (seq != hwc->interrupts_seq) {
  5007. hwc->interrupts_seq = seq;
  5008. hwc->interrupts = 1;
  5009. } else {
  5010. hwc->interrupts++;
  5011. if (unlikely(throttle
  5012. && hwc->interrupts >= max_samples_per_tick)) {
  5013. __this_cpu_inc(perf_throttled_count);
  5014. hwc->interrupts = MAX_INTERRUPTS;
  5015. perf_log_throttle(event, 0);
  5016. tick_nohz_full_kick();
  5017. ret = 1;
  5018. }
  5019. }
  5020. if (event->attr.freq) {
  5021. u64 now = perf_clock();
  5022. s64 delta = now - hwc->freq_time_stamp;
  5023. hwc->freq_time_stamp = now;
  5024. if (delta > 0 && delta < 2*TICK_NSEC)
  5025. perf_adjust_period(event, delta, hwc->last_period, true);
  5026. }
  5027. /*
  5028. * XXX event_limit might not quite work as expected on inherited
  5029. * events
  5030. */
  5031. event->pending_kill = POLL_IN;
  5032. if (events && atomic_dec_and_test(&event->event_limit)) {
  5033. ret = 1;
  5034. event->pending_kill = POLL_HUP;
  5035. event->pending_disable = 1;
  5036. irq_work_queue(&event->pending);
  5037. }
  5038. if (event->overflow_handler)
  5039. event->overflow_handler(event, data, regs);
  5040. else
  5041. perf_event_output(event, data, regs);
  5042. if (event->fasync && event->pending_kill) {
  5043. event->pending_wakeup = 1;
  5044. irq_work_queue(&event->pending);
  5045. }
  5046. return ret;
  5047. }
  5048. int perf_event_overflow(struct perf_event *event,
  5049. struct perf_sample_data *data,
  5050. struct pt_regs *regs)
  5051. {
  5052. return __perf_event_overflow(event, 1, data, regs);
  5053. }
  5054. /*
  5055. * Generic software event infrastructure
  5056. */
  5057. struct swevent_htable {
  5058. struct swevent_hlist *swevent_hlist;
  5059. struct mutex hlist_mutex;
  5060. int hlist_refcount;
  5061. /* Recursion avoidance in each contexts */
  5062. int recursion[PERF_NR_CONTEXTS];
  5063. /* Keeps track of cpu being initialized/exited */
  5064. bool online;
  5065. };
  5066. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  5067. /*
  5068. * We directly increment event->count and keep a second value in
  5069. * event->hw.period_left to count intervals. This period event
  5070. * is kept in the range [-sample_period, 0] so that we can use the
  5071. * sign as trigger.
  5072. */
  5073. u64 perf_swevent_set_period(struct perf_event *event)
  5074. {
  5075. struct hw_perf_event *hwc = &event->hw;
  5076. u64 period = hwc->last_period;
  5077. u64 nr, offset;
  5078. s64 old, val;
  5079. hwc->last_period = hwc->sample_period;
  5080. again:
  5081. old = val = local64_read(&hwc->period_left);
  5082. if (val < 0)
  5083. return 0;
  5084. nr = div64_u64(period + val, period);
  5085. offset = nr * period;
  5086. val -= offset;
  5087. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  5088. goto again;
  5089. return nr;
  5090. }
  5091. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  5092. struct perf_sample_data *data,
  5093. struct pt_regs *regs)
  5094. {
  5095. struct hw_perf_event *hwc = &event->hw;
  5096. int throttle = 0;
  5097. if (!overflow)
  5098. overflow = perf_swevent_set_period(event);
  5099. if (hwc->interrupts == MAX_INTERRUPTS)
  5100. return;
  5101. for (; overflow; overflow--) {
  5102. if (__perf_event_overflow(event, throttle,
  5103. data, regs)) {
  5104. /*
  5105. * We inhibit the overflow from happening when
  5106. * hwc->interrupts == MAX_INTERRUPTS.
  5107. */
  5108. break;
  5109. }
  5110. throttle = 1;
  5111. }
  5112. }
  5113. static void perf_swevent_event(struct perf_event *event, u64 nr,
  5114. struct perf_sample_data *data,
  5115. struct pt_regs *regs)
  5116. {
  5117. struct hw_perf_event *hwc = &event->hw;
  5118. local64_add(nr, &event->count);
  5119. if (!regs)
  5120. return;
  5121. if (!is_sampling_event(event))
  5122. return;
  5123. if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
  5124. data->period = nr;
  5125. return perf_swevent_overflow(event, 1, data, regs);
  5126. } else
  5127. data->period = event->hw.last_period;
  5128. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  5129. return perf_swevent_overflow(event, 1, data, regs);
  5130. if (local64_add_negative(nr, &hwc->period_left))
  5131. return;
  5132. perf_swevent_overflow(event, 0, data, regs);
  5133. }
  5134. static int perf_exclude_event(struct perf_event *event,
  5135. struct pt_regs *regs)
  5136. {
  5137. if (event->hw.state & PERF_HES_STOPPED)
  5138. return 1;
  5139. if (regs) {
  5140. if (event->attr.exclude_user && user_mode(regs))
  5141. return 1;
  5142. if (event->attr.exclude_kernel && !user_mode(regs))
  5143. return 1;
  5144. }
  5145. return 0;
  5146. }
  5147. static int perf_swevent_match(struct perf_event *event,
  5148. enum perf_type_id type,
  5149. u32 event_id,
  5150. struct perf_sample_data *data,
  5151. struct pt_regs *regs)
  5152. {
  5153. if (event->attr.type != type)
  5154. return 0;
  5155. if (event->attr.config != event_id)
  5156. return 0;
  5157. if (perf_exclude_event(event, regs))
  5158. return 0;
  5159. return 1;
  5160. }
  5161. static inline u64 swevent_hash(u64 type, u32 event_id)
  5162. {
  5163. u64 val = event_id | (type << 32);
  5164. return hash_64(val, SWEVENT_HLIST_BITS);
  5165. }
  5166. static inline struct hlist_head *
  5167. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  5168. {
  5169. u64 hash = swevent_hash(type, event_id);
  5170. return &hlist->heads[hash];
  5171. }
  5172. /* For the read side: events when they trigger */
  5173. static inline struct hlist_head *
  5174. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  5175. {
  5176. struct swevent_hlist *hlist;
  5177. hlist = rcu_dereference(swhash->swevent_hlist);
  5178. if (!hlist)
  5179. return NULL;
  5180. return __find_swevent_head(hlist, type, event_id);
  5181. }
  5182. /* For the event head insertion and removal in the hlist */
  5183. static inline struct hlist_head *
  5184. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  5185. {
  5186. struct swevent_hlist *hlist;
  5187. u32 event_id = event->attr.config;
  5188. u64 type = event->attr.type;
  5189. /*
  5190. * Event scheduling is always serialized against hlist allocation
  5191. * and release. Which makes the protected version suitable here.
  5192. * The context lock guarantees that.
  5193. */
  5194. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  5195. lockdep_is_held(&event->ctx->lock));
  5196. if (!hlist)
  5197. return NULL;
  5198. return __find_swevent_head(hlist, type, event_id);
  5199. }
  5200. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  5201. u64 nr,
  5202. struct perf_sample_data *data,
  5203. struct pt_regs *regs)
  5204. {
  5205. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5206. struct perf_event *event;
  5207. struct hlist_head *head;
  5208. rcu_read_lock();
  5209. head = find_swevent_head_rcu(swhash, type, event_id);
  5210. if (!head)
  5211. goto end;
  5212. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  5213. if (perf_swevent_match(event, type, event_id, data, regs))
  5214. perf_swevent_event(event, nr, data, regs);
  5215. }
  5216. end:
  5217. rcu_read_unlock();
  5218. }
  5219. DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
  5220. int perf_swevent_get_recursion_context(void)
  5221. {
  5222. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5223. return get_recursion_context(swhash->recursion);
  5224. }
  5225. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  5226. inline void perf_swevent_put_recursion_context(int rctx)
  5227. {
  5228. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5229. put_recursion_context(swhash->recursion, rctx);
  5230. }
  5231. void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  5232. {
  5233. struct perf_sample_data data;
  5234. if (WARN_ON_ONCE(!regs))
  5235. return;
  5236. perf_sample_data_init(&data, addr, 0);
  5237. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
  5238. }
  5239. void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  5240. {
  5241. int rctx;
  5242. preempt_disable_notrace();
  5243. rctx = perf_swevent_get_recursion_context();
  5244. if (unlikely(rctx < 0))
  5245. goto fail;
  5246. ___perf_sw_event(event_id, nr, regs, addr);
  5247. perf_swevent_put_recursion_context(rctx);
  5248. fail:
  5249. preempt_enable_notrace();
  5250. }
  5251. static void perf_swevent_read(struct perf_event *event)
  5252. {
  5253. }
  5254. static int perf_swevent_add(struct perf_event *event, int flags)
  5255. {
  5256. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  5257. struct hw_perf_event *hwc = &event->hw;
  5258. struct hlist_head *head;
  5259. if (is_sampling_event(event)) {
  5260. hwc->last_period = hwc->sample_period;
  5261. perf_swevent_set_period(event);
  5262. }
  5263. hwc->state = !(flags & PERF_EF_START);
  5264. head = find_swevent_head(swhash, event);
  5265. if (!head) {
  5266. /*
  5267. * We can race with cpu hotplug code. Do not
  5268. * WARN if the cpu just got unplugged.
  5269. */
  5270. WARN_ON_ONCE(swhash->online);
  5271. return -EINVAL;
  5272. }
  5273. hlist_add_head_rcu(&event->hlist_entry, head);
  5274. perf_event_update_userpage(event);
  5275. return 0;
  5276. }
  5277. static void perf_swevent_del(struct perf_event *event, int flags)
  5278. {
  5279. hlist_del_rcu(&event->hlist_entry);
  5280. }
  5281. static void perf_swevent_start(struct perf_event *event, int flags)
  5282. {
  5283. event->hw.state = 0;
  5284. }
  5285. static void perf_swevent_stop(struct perf_event *event, int flags)
  5286. {
  5287. event->hw.state = PERF_HES_STOPPED;
  5288. }
  5289. /* Deref the hlist from the update side */
  5290. static inline struct swevent_hlist *
  5291. swevent_hlist_deref(struct swevent_htable *swhash)
  5292. {
  5293. return rcu_dereference_protected(swhash->swevent_hlist,
  5294. lockdep_is_held(&swhash->hlist_mutex));
  5295. }
  5296. static void swevent_hlist_release(struct swevent_htable *swhash)
  5297. {
  5298. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  5299. if (!hlist)
  5300. return;
  5301. RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
  5302. kfree_rcu(hlist, rcu_head);
  5303. }
  5304. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  5305. {
  5306. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5307. mutex_lock(&swhash->hlist_mutex);
  5308. if (!--swhash->hlist_refcount)
  5309. swevent_hlist_release(swhash);
  5310. mutex_unlock(&swhash->hlist_mutex);
  5311. }
  5312. static void swevent_hlist_put(struct perf_event *event)
  5313. {
  5314. int cpu;
  5315. for_each_possible_cpu(cpu)
  5316. swevent_hlist_put_cpu(event, cpu);
  5317. }
  5318. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  5319. {
  5320. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5321. int err = 0;
  5322. mutex_lock(&swhash->hlist_mutex);
  5323. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  5324. struct swevent_hlist *hlist;
  5325. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  5326. if (!hlist) {
  5327. err = -ENOMEM;
  5328. goto exit;
  5329. }
  5330. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5331. }
  5332. swhash->hlist_refcount++;
  5333. exit:
  5334. mutex_unlock(&swhash->hlist_mutex);
  5335. return err;
  5336. }
  5337. static int swevent_hlist_get(struct perf_event *event)
  5338. {
  5339. int err;
  5340. int cpu, failed_cpu;
  5341. get_online_cpus();
  5342. for_each_possible_cpu(cpu) {
  5343. err = swevent_hlist_get_cpu(event, cpu);
  5344. if (err) {
  5345. failed_cpu = cpu;
  5346. goto fail;
  5347. }
  5348. }
  5349. put_online_cpus();
  5350. return 0;
  5351. fail:
  5352. for_each_possible_cpu(cpu) {
  5353. if (cpu == failed_cpu)
  5354. break;
  5355. swevent_hlist_put_cpu(event, cpu);
  5356. }
  5357. put_online_cpus();
  5358. return err;
  5359. }
  5360. struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  5361. static void sw_perf_event_destroy(struct perf_event *event)
  5362. {
  5363. u64 event_id = event->attr.config;
  5364. WARN_ON(event->parent);
  5365. static_key_slow_dec(&perf_swevent_enabled[event_id]);
  5366. swevent_hlist_put(event);
  5367. }
  5368. static int perf_swevent_init(struct perf_event *event)
  5369. {
  5370. u64 event_id = event->attr.config;
  5371. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5372. return -ENOENT;
  5373. /*
  5374. * no branch sampling for software events
  5375. */
  5376. if (has_branch_stack(event))
  5377. return -EOPNOTSUPP;
  5378. switch (event_id) {
  5379. case PERF_COUNT_SW_CPU_CLOCK:
  5380. case PERF_COUNT_SW_TASK_CLOCK:
  5381. return -ENOENT;
  5382. default:
  5383. break;
  5384. }
  5385. if (event_id >= PERF_COUNT_SW_MAX)
  5386. return -ENOENT;
  5387. if (!event->parent) {
  5388. int err;
  5389. err = swevent_hlist_get(event);
  5390. if (err)
  5391. return err;
  5392. static_key_slow_inc(&perf_swevent_enabled[event_id]);
  5393. event->destroy = sw_perf_event_destroy;
  5394. }
  5395. return 0;
  5396. }
  5397. static struct pmu perf_swevent = {
  5398. .task_ctx_nr = perf_sw_context,
  5399. .capabilities = PERF_PMU_CAP_NO_NMI,
  5400. .event_init = perf_swevent_init,
  5401. .add = perf_swevent_add,
  5402. .del = perf_swevent_del,
  5403. .start = perf_swevent_start,
  5404. .stop = perf_swevent_stop,
  5405. .read = perf_swevent_read,
  5406. };
  5407. #ifdef CONFIG_EVENT_TRACING
  5408. static int perf_tp_filter_match(struct perf_event *event,
  5409. struct perf_sample_data *data)
  5410. {
  5411. void *record = data->raw->data;
  5412. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  5413. return 1;
  5414. return 0;
  5415. }
  5416. static int perf_tp_event_match(struct perf_event *event,
  5417. struct perf_sample_data *data,
  5418. struct pt_regs *regs)
  5419. {
  5420. if (event->hw.state & PERF_HES_STOPPED)
  5421. return 0;
  5422. /*
  5423. * All tracepoints are from kernel-space.
  5424. */
  5425. if (event->attr.exclude_kernel)
  5426. return 0;
  5427. if (!perf_tp_filter_match(event, data))
  5428. return 0;
  5429. return 1;
  5430. }
  5431. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  5432. struct pt_regs *regs, struct hlist_head *head, int rctx,
  5433. struct task_struct *task)
  5434. {
  5435. struct perf_sample_data data;
  5436. struct perf_event *event;
  5437. struct perf_raw_record raw = {
  5438. .size = entry_size,
  5439. .data = record,
  5440. };
  5441. perf_sample_data_init(&data, addr, 0);
  5442. data.raw = &raw;
  5443. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  5444. if (perf_tp_event_match(event, &data, regs))
  5445. perf_swevent_event(event, count, &data, regs);
  5446. }
  5447. /*
  5448. * If we got specified a target task, also iterate its context and
  5449. * deliver this event there too.
  5450. */
  5451. if (task && task != current) {
  5452. struct perf_event_context *ctx;
  5453. struct trace_entry *entry = record;
  5454. rcu_read_lock();
  5455. ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
  5456. if (!ctx)
  5457. goto unlock;
  5458. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  5459. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5460. continue;
  5461. if (event->attr.config != entry->type)
  5462. continue;
  5463. if (perf_tp_event_match(event, &data, regs))
  5464. perf_swevent_event(event, count, &data, regs);
  5465. }
  5466. unlock:
  5467. rcu_read_unlock();
  5468. }
  5469. perf_swevent_put_recursion_context(rctx);
  5470. }
  5471. EXPORT_SYMBOL_GPL(perf_tp_event);
  5472. static void tp_perf_event_destroy(struct perf_event *event)
  5473. {
  5474. perf_trace_destroy(event);
  5475. }
  5476. static int perf_tp_event_init(struct perf_event *event)
  5477. {
  5478. int err;
  5479. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5480. return -ENOENT;
  5481. /*
  5482. * no branch sampling for tracepoint events
  5483. */
  5484. if (has_branch_stack(event))
  5485. return -EOPNOTSUPP;
  5486. err = perf_trace_init(event);
  5487. if (err)
  5488. return err;
  5489. event->destroy = tp_perf_event_destroy;
  5490. return 0;
  5491. }
  5492. static struct pmu perf_tracepoint = {
  5493. .task_ctx_nr = perf_sw_context,
  5494. .event_init = perf_tp_event_init,
  5495. .add = perf_trace_add,
  5496. .del = perf_trace_del,
  5497. .start = perf_swevent_start,
  5498. .stop = perf_swevent_stop,
  5499. .read = perf_swevent_read,
  5500. };
  5501. static inline void perf_tp_register(void)
  5502. {
  5503. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  5504. }
  5505. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  5506. {
  5507. char *filter_str;
  5508. int ret;
  5509. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5510. return -EINVAL;
  5511. filter_str = strndup_user(arg, PAGE_SIZE);
  5512. if (IS_ERR(filter_str))
  5513. return PTR_ERR(filter_str);
  5514. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  5515. kfree(filter_str);
  5516. return ret;
  5517. }
  5518. static void perf_event_free_filter(struct perf_event *event)
  5519. {
  5520. ftrace_profile_free_filter(event);
  5521. }
  5522. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
  5523. {
  5524. struct bpf_prog *prog;
  5525. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  5526. return -EINVAL;
  5527. if (event->tp_event->prog)
  5528. return -EEXIST;
  5529. if (!(event->tp_event->flags & TRACE_EVENT_FL_KPROBE))
  5530. /* bpf programs can only be attached to kprobes */
  5531. return -EINVAL;
  5532. prog = bpf_prog_get(prog_fd);
  5533. if (IS_ERR(prog))
  5534. return PTR_ERR(prog);
  5535. if (prog->type != BPF_PROG_TYPE_KPROBE) {
  5536. /* valid fd, but invalid bpf program type */
  5537. bpf_prog_put(prog);
  5538. return -EINVAL;
  5539. }
  5540. event->tp_event->prog = prog;
  5541. return 0;
  5542. }
  5543. static void perf_event_free_bpf_prog(struct perf_event *event)
  5544. {
  5545. struct bpf_prog *prog;
  5546. if (!event->tp_event)
  5547. return;
  5548. prog = event->tp_event->prog;
  5549. if (prog) {
  5550. event->tp_event->prog = NULL;
  5551. bpf_prog_put(prog);
  5552. }
  5553. }
  5554. #else
  5555. static inline void perf_tp_register(void)
  5556. {
  5557. }
  5558. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  5559. {
  5560. return -ENOENT;
  5561. }
  5562. static void perf_event_free_filter(struct perf_event *event)
  5563. {
  5564. }
  5565. static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
  5566. {
  5567. return -ENOENT;
  5568. }
  5569. static void perf_event_free_bpf_prog(struct perf_event *event)
  5570. {
  5571. }
  5572. #endif /* CONFIG_EVENT_TRACING */
  5573. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  5574. void perf_bp_event(struct perf_event *bp, void *data)
  5575. {
  5576. struct perf_sample_data sample;
  5577. struct pt_regs *regs = data;
  5578. perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
  5579. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  5580. perf_swevent_event(bp, 1, &sample, regs);
  5581. }
  5582. #endif
  5583. /*
  5584. * hrtimer based swevent callback
  5585. */
  5586. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  5587. {
  5588. enum hrtimer_restart ret = HRTIMER_RESTART;
  5589. struct perf_sample_data data;
  5590. struct pt_regs *regs;
  5591. struct perf_event *event;
  5592. u64 period;
  5593. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  5594. if (event->state != PERF_EVENT_STATE_ACTIVE)
  5595. return HRTIMER_NORESTART;
  5596. event->pmu->read(event);
  5597. perf_sample_data_init(&data, 0, event->hw.last_period);
  5598. regs = get_irq_regs();
  5599. if (regs && !perf_exclude_event(event, regs)) {
  5600. if (!(event->attr.exclude_idle && is_idle_task(current)))
  5601. if (__perf_event_overflow(event, 1, &data, regs))
  5602. ret = HRTIMER_NORESTART;
  5603. }
  5604. period = max_t(u64, 10000, event->hw.sample_period);
  5605. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  5606. return ret;
  5607. }
  5608. static void perf_swevent_start_hrtimer(struct perf_event *event)
  5609. {
  5610. struct hw_perf_event *hwc = &event->hw;
  5611. s64 period;
  5612. if (!is_sampling_event(event))
  5613. return;
  5614. period = local64_read(&hwc->period_left);
  5615. if (period) {
  5616. if (period < 0)
  5617. period = 10000;
  5618. local64_set(&hwc->period_left, 0);
  5619. } else {
  5620. period = max_t(u64, 10000, hwc->sample_period);
  5621. }
  5622. hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
  5623. HRTIMER_MODE_REL_PINNED);
  5624. }
  5625. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  5626. {
  5627. struct hw_perf_event *hwc = &event->hw;
  5628. if (is_sampling_event(event)) {
  5629. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  5630. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  5631. hrtimer_cancel(&hwc->hrtimer);
  5632. }
  5633. }
  5634. static void perf_swevent_init_hrtimer(struct perf_event *event)
  5635. {
  5636. struct hw_perf_event *hwc = &event->hw;
  5637. if (!is_sampling_event(event))
  5638. return;
  5639. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  5640. hwc->hrtimer.function = perf_swevent_hrtimer;
  5641. /*
  5642. * Since hrtimers have a fixed rate, we can do a static freq->period
  5643. * mapping and avoid the whole period adjust feedback stuff.
  5644. */
  5645. if (event->attr.freq) {
  5646. long freq = event->attr.sample_freq;
  5647. event->attr.sample_period = NSEC_PER_SEC / freq;
  5648. hwc->sample_period = event->attr.sample_period;
  5649. local64_set(&hwc->period_left, hwc->sample_period);
  5650. hwc->last_period = hwc->sample_period;
  5651. event->attr.freq = 0;
  5652. }
  5653. }
  5654. /*
  5655. * Software event: cpu wall time clock
  5656. */
  5657. static void cpu_clock_event_update(struct perf_event *event)
  5658. {
  5659. s64 prev;
  5660. u64 now;
  5661. now = local_clock();
  5662. prev = local64_xchg(&event->hw.prev_count, now);
  5663. local64_add(now - prev, &event->count);
  5664. }
  5665. static void cpu_clock_event_start(struct perf_event *event, int flags)
  5666. {
  5667. local64_set(&event->hw.prev_count, local_clock());
  5668. perf_swevent_start_hrtimer(event);
  5669. }
  5670. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  5671. {
  5672. perf_swevent_cancel_hrtimer(event);
  5673. cpu_clock_event_update(event);
  5674. }
  5675. static int cpu_clock_event_add(struct perf_event *event, int flags)
  5676. {
  5677. if (flags & PERF_EF_START)
  5678. cpu_clock_event_start(event, flags);
  5679. perf_event_update_userpage(event);
  5680. return 0;
  5681. }
  5682. static void cpu_clock_event_del(struct perf_event *event, int flags)
  5683. {
  5684. cpu_clock_event_stop(event, flags);
  5685. }
  5686. static void cpu_clock_event_read(struct perf_event *event)
  5687. {
  5688. cpu_clock_event_update(event);
  5689. }
  5690. static int cpu_clock_event_init(struct perf_event *event)
  5691. {
  5692. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5693. return -ENOENT;
  5694. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  5695. return -ENOENT;
  5696. /*
  5697. * no branch sampling for software events
  5698. */
  5699. if (has_branch_stack(event))
  5700. return -EOPNOTSUPP;
  5701. perf_swevent_init_hrtimer(event);
  5702. return 0;
  5703. }
  5704. static struct pmu perf_cpu_clock = {
  5705. .task_ctx_nr = perf_sw_context,
  5706. .capabilities = PERF_PMU_CAP_NO_NMI,
  5707. .event_init = cpu_clock_event_init,
  5708. .add = cpu_clock_event_add,
  5709. .del = cpu_clock_event_del,
  5710. .start = cpu_clock_event_start,
  5711. .stop = cpu_clock_event_stop,
  5712. .read = cpu_clock_event_read,
  5713. };
  5714. /*
  5715. * Software event: task time clock
  5716. */
  5717. static void task_clock_event_update(struct perf_event *event, u64 now)
  5718. {
  5719. u64 prev;
  5720. s64 delta;
  5721. prev = local64_xchg(&event->hw.prev_count, now);
  5722. delta = now - prev;
  5723. local64_add(delta, &event->count);
  5724. }
  5725. static void task_clock_event_start(struct perf_event *event, int flags)
  5726. {
  5727. local64_set(&event->hw.prev_count, event->ctx->time);
  5728. perf_swevent_start_hrtimer(event);
  5729. }
  5730. static void task_clock_event_stop(struct perf_event *event, int flags)
  5731. {
  5732. perf_swevent_cancel_hrtimer(event);
  5733. task_clock_event_update(event, event->ctx->time);
  5734. }
  5735. static int task_clock_event_add(struct perf_event *event, int flags)
  5736. {
  5737. if (flags & PERF_EF_START)
  5738. task_clock_event_start(event, flags);
  5739. perf_event_update_userpage(event);
  5740. return 0;
  5741. }
  5742. static void task_clock_event_del(struct perf_event *event, int flags)
  5743. {
  5744. task_clock_event_stop(event, PERF_EF_UPDATE);
  5745. }
  5746. static void task_clock_event_read(struct perf_event *event)
  5747. {
  5748. u64 now = perf_clock();
  5749. u64 delta = now - event->ctx->timestamp;
  5750. u64 time = event->ctx->time + delta;
  5751. task_clock_event_update(event, time);
  5752. }
  5753. static int task_clock_event_init(struct perf_event *event)
  5754. {
  5755. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5756. return -ENOENT;
  5757. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  5758. return -ENOENT;
  5759. /*
  5760. * no branch sampling for software events
  5761. */
  5762. if (has_branch_stack(event))
  5763. return -EOPNOTSUPP;
  5764. perf_swevent_init_hrtimer(event);
  5765. return 0;
  5766. }
  5767. static struct pmu perf_task_clock = {
  5768. .task_ctx_nr = perf_sw_context,
  5769. .capabilities = PERF_PMU_CAP_NO_NMI,
  5770. .event_init = task_clock_event_init,
  5771. .add = task_clock_event_add,
  5772. .del = task_clock_event_del,
  5773. .start = task_clock_event_start,
  5774. .stop = task_clock_event_stop,
  5775. .read = task_clock_event_read,
  5776. };
  5777. static void perf_pmu_nop_void(struct pmu *pmu)
  5778. {
  5779. }
  5780. static int perf_pmu_nop_int(struct pmu *pmu)
  5781. {
  5782. return 0;
  5783. }
  5784. static void perf_pmu_start_txn(struct pmu *pmu)
  5785. {
  5786. perf_pmu_disable(pmu);
  5787. }
  5788. static int perf_pmu_commit_txn(struct pmu *pmu)
  5789. {
  5790. perf_pmu_enable(pmu);
  5791. return 0;
  5792. }
  5793. static void perf_pmu_cancel_txn(struct pmu *pmu)
  5794. {
  5795. perf_pmu_enable(pmu);
  5796. }
  5797. static int perf_event_idx_default(struct perf_event *event)
  5798. {
  5799. return 0;
  5800. }
  5801. /*
  5802. * Ensures all contexts with the same task_ctx_nr have the same
  5803. * pmu_cpu_context too.
  5804. */
  5805. static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
  5806. {
  5807. struct pmu *pmu;
  5808. if (ctxn < 0)
  5809. return NULL;
  5810. list_for_each_entry(pmu, &pmus, entry) {
  5811. if (pmu->task_ctx_nr == ctxn)
  5812. return pmu->pmu_cpu_context;
  5813. }
  5814. return NULL;
  5815. }
  5816. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  5817. {
  5818. int cpu;
  5819. for_each_possible_cpu(cpu) {
  5820. struct perf_cpu_context *cpuctx;
  5821. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5822. if (cpuctx->unique_pmu == old_pmu)
  5823. cpuctx->unique_pmu = pmu;
  5824. }
  5825. }
  5826. static void free_pmu_context(struct pmu *pmu)
  5827. {
  5828. struct pmu *i;
  5829. mutex_lock(&pmus_lock);
  5830. /*
  5831. * Like a real lame refcount.
  5832. */
  5833. list_for_each_entry(i, &pmus, entry) {
  5834. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  5835. update_pmu_context(i, pmu);
  5836. goto out;
  5837. }
  5838. }
  5839. free_percpu(pmu->pmu_cpu_context);
  5840. out:
  5841. mutex_unlock(&pmus_lock);
  5842. }
  5843. static struct idr pmu_idr;
  5844. static ssize_t
  5845. type_show(struct device *dev, struct device_attribute *attr, char *page)
  5846. {
  5847. struct pmu *pmu = dev_get_drvdata(dev);
  5848. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  5849. }
  5850. static DEVICE_ATTR_RO(type);
  5851. static ssize_t
  5852. perf_event_mux_interval_ms_show(struct device *dev,
  5853. struct device_attribute *attr,
  5854. char *page)
  5855. {
  5856. struct pmu *pmu = dev_get_drvdata(dev);
  5857. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
  5858. }
  5859. static DEFINE_MUTEX(mux_interval_mutex);
  5860. static ssize_t
  5861. perf_event_mux_interval_ms_store(struct device *dev,
  5862. struct device_attribute *attr,
  5863. const char *buf, size_t count)
  5864. {
  5865. struct pmu *pmu = dev_get_drvdata(dev);
  5866. int timer, cpu, ret;
  5867. ret = kstrtoint(buf, 0, &timer);
  5868. if (ret)
  5869. return ret;
  5870. if (timer < 1)
  5871. return -EINVAL;
  5872. /* same value, noting to do */
  5873. if (timer == pmu->hrtimer_interval_ms)
  5874. return count;
  5875. mutex_lock(&mux_interval_mutex);
  5876. pmu->hrtimer_interval_ms = timer;
  5877. /* update all cpuctx for this PMU */
  5878. get_online_cpus();
  5879. for_each_online_cpu(cpu) {
  5880. struct perf_cpu_context *cpuctx;
  5881. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5882. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
  5883. cpu_function_call(cpu,
  5884. (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
  5885. }
  5886. put_online_cpus();
  5887. mutex_unlock(&mux_interval_mutex);
  5888. return count;
  5889. }
  5890. static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
  5891. static struct attribute *pmu_dev_attrs[] = {
  5892. &dev_attr_type.attr,
  5893. &dev_attr_perf_event_mux_interval_ms.attr,
  5894. NULL,
  5895. };
  5896. ATTRIBUTE_GROUPS(pmu_dev);
  5897. static int pmu_bus_running;
  5898. static struct bus_type pmu_bus = {
  5899. .name = "event_source",
  5900. .dev_groups = pmu_dev_groups,
  5901. };
  5902. static void pmu_dev_release(struct device *dev)
  5903. {
  5904. kfree(dev);
  5905. }
  5906. static int pmu_dev_alloc(struct pmu *pmu)
  5907. {
  5908. int ret = -ENOMEM;
  5909. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  5910. if (!pmu->dev)
  5911. goto out;
  5912. pmu->dev->groups = pmu->attr_groups;
  5913. device_initialize(pmu->dev);
  5914. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  5915. if (ret)
  5916. goto free_dev;
  5917. dev_set_drvdata(pmu->dev, pmu);
  5918. pmu->dev->bus = &pmu_bus;
  5919. pmu->dev->release = pmu_dev_release;
  5920. ret = device_add(pmu->dev);
  5921. if (ret)
  5922. goto free_dev;
  5923. out:
  5924. return ret;
  5925. free_dev:
  5926. put_device(pmu->dev);
  5927. goto out;
  5928. }
  5929. static struct lock_class_key cpuctx_mutex;
  5930. static struct lock_class_key cpuctx_lock;
  5931. int perf_pmu_register(struct pmu *pmu, const char *name, int type)
  5932. {
  5933. int cpu, ret;
  5934. mutex_lock(&pmus_lock);
  5935. ret = -ENOMEM;
  5936. pmu->pmu_disable_count = alloc_percpu(int);
  5937. if (!pmu->pmu_disable_count)
  5938. goto unlock;
  5939. pmu->type = -1;
  5940. if (!name)
  5941. goto skip_type;
  5942. pmu->name = name;
  5943. if (type < 0) {
  5944. type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
  5945. if (type < 0) {
  5946. ret = type;
  5947. goto free_pdc;
  5948. }
  5949. }
  5950. pmu->type = type;
  5951. if (pmu_bus_running) {
  5952. ret = pmu_dev_alloc(pmu);
  5953. if (ret)
  5954. goto free_idr;
  5955. }
  5956. skip_type:
  5957. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  5958. if (pmu->pmu_cpu_context)
  5959. goto got_cpu_context;
  5960. ret = -ENOMEM;
  5961. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  5962. if (!pmu->pmu_cpu_context)
  5963. goto free_dev;
  5964. for_each_possible_cpu(cpu) {
  5965. struct perf_cpu_context *cpuctx;
  5966. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5967. __perf_event_init_context(&cpuctx->ctx);
  5968. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  5969. lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
  5970. cpuctx->ctx.pmu = pmu;
  5971. __perf_mux_hrtimer_init(cpuctx, cpu);
  5972. cpuctx->unique_pmu = pmu;
  5973. }
  5974. got_cpu_context:
  5975. if (!pmu->start_txn) {
  5976. if (pmu->pmu_enable) {
  5977. /*
  5978. * If we have pmu_enable/pmu_disable calls, install
  5979. * transaction stubs that use that to try and batch
  5980. * hardware accesses.
  5981. */
  5982. pmu->start_txn = perf_pmu_start_txn;
  5983. pmu->commit_txn = perf_pmu_commit_txn;
  5984. pmu->cancel_txn = perf_pmu_cancel_txn;
  5985. } else {
  5986. pmu->start_txn = perf_pmu_nop_void;
  5987. pmu->commit_txn = perf_pmu_nop_int;
  5988. pmu->cancel_txn = perf_pmu_nop_void;
  5989. }
  5990. }
  5991. if (!pmu->pmu_enable) {
  5992. pmu->pmu_enable = perf_pmu_nop_void;
  5993. pmu->pmu_disable = perf_pmu_nop_void;
  5994. }
  5995. if (!pmu->event_idx)
  5996. pmu->event_idx = perf_event_idx_default;
  5997. list_add_rcu(&pmu->entry, &pmus);
  5998. atomic_set(&pmu->exclusive_cnt, 0);
  5999. ret = 0;
  6000. unlock:
  6001. mutex_unlock(&pmus_lock);
  6002. return ret;
  6003. free_dev:
  6004. device_del(pmu->dev);
  6005. put_device(pmu->dev);
  6006. free_idr:
  6007. if (pmu->type >= PERF_TYPE_MAX)
  6008. idr_remove(&pmu_idr, pmu->type);
  6009. free_pdc:
  6010. free_percpu(pmu->pmu_disable_count);
  6011. goto unlock;
  6012. }
  6013. EXPORT_SYMBOL_GPL(perf_pmu_register);
  6014. void perf_pmu_unregister(struct pmu *pmu)
  6015. {
  6016. mutex_lock(&pmus_lock);
  6017. list_del_rcu(&pmu->entry);
  6018. mutex_unlock(&pmus_lock);
  6019. /*
  6020. * We dereference the pmu list under both SRCU and regular RCU, so
  6021. * synchronize against both of those.
  6022. */
  6023. synchronize_srcu(&pmus_srcu);
  6024. synchronize_rcu();
  6025. free_percpu(pmu->pmu_disable_count);
  6026. if (pmu->type >= PERF_TYPE_MAX)
  6027. idr_remove(&pmu_idr, pmu->type);
  6028. device_del(pmu->dev);
  6029. put_device(pmu->dev);
  6030. free_pmu_context(pmu);
  6031. }
  6032. EXPORT_SYMBOL_GPL(perf_pmu_unregister);
  6033. static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
  6034. {
  6035. struct perf_event_context *ctx = NULL;
  6036. int ret;
  6037. if (!try_module_get(pmu->module))
  6038. return -ENODEV;
  6039. if (event->group_leader != event) {
  6040. /*
  6041. * This ctx->mutex can nest when we're called through
  6042. * inheritance. See the perf_event_ctx_lock_nested() comment.
  6043. */
  6044. ctx = perf_event_ctx_lock_nested(event->group_leader,
  6045. SINGLE_DEPTH_NESTING);
  6046. BUG_ON(!ctx);
  6047. }
  6048. event->pmu = pmu;
  6049. ret = pmu->event_init(event);
  6050. if (ctx)
  6051. perf_event_ctx_unlock(event->group_leader, ctx);
  6052. if (ret)
  6053. module_put(pmu->module);
  6054. return ret;
  6055. }
  6056. struct pmu *perf_init_event(struct perf_event *event)
  6057. {
  6058. struct pmu *pmu = NULL;
  6059. int idx;
  6060. int ret;
  6061. idx = srcu_read_lock(&pmus_srcu);
  6062. rcu_read_lock();
  6063. pmu = idr_find(&pmu_idr, event->attr.type);
  6064. rcu_read_unlock();
  6065. if (pmu) {
  6066. ret = perf_try_init_event(pmu, event);
  6067. if (ret)
  6068. pmu = ERR_PTR(ret);
  6069. goto unlock;
  6070. }
  6071. list_for_each_entry_rcu(pmu, &pmus, entry) {
  6072. ret = perf_try_init_event(pmu, event);
  6073. if (!ret)
  6074. goto unlock;
  6075. if (ret != -ENOENT) {
  6076. pmu = ERR_PTR(ret);
  6077. goto unlock;
  6078. }
  6079. }
  6080. pmu = ERR_PTR(-ENOENT);
  6081. unlock:
  6082. srcu_read_unlock(&pmus_srcu, idx);
  6083. return pmu;
  6084. }
  6085. static void account_event_cpu(struct perf_event *event, int cpu)
  6086. {
  6087. if (event->parent)
  6088. return;
  6089. if (is_cgroup_event(event))
  6090. atomic_inc(&per_cpu(perf_cgroup_events, cpu));
  6091. }
  6092. static void account_event(struct perf_event *event)
  6093. {
  6094. if (event->parent)
  6095. return;
  6096. if (event->attach_state & PERF_ATTACH_TASK)
  6097. static_key_slow_inc(&perf_sched_events.key);
  6098. if (event->attr.mmap || event->attr.mmap_data)
  6099. atomic_inc(&nr_mmap_events);
  6100. if (event->attr.comm)
  6101. atomic_inc(&nr_comm_events);
  6102. if (event->attr.task)
  6103. atomic_inc(&nr_task_events);
  6104. if (event->attr.freq) {
  6105. if (atomic_inc_return(&nr_freq_events) == 1)
  6106. tick_nohz_full_kick_all();
  6107. }
  6108. if (has_branch_stack(event))
  6109. static_key_slow_inc(&perf_sched_events.key);
  6110. if (is_cgroup_event(event))
  6111. static_key_slow_inc(&perf_sched_events.key);
  6112. account_event_cpu(event, event->cpu);
  6113. }
  6114. /*
  6115. * Allocate and initialize a event structure
  6116. */
  6117. static struct perf_event *
  6118. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  6119. struct task_struct *task,
  6120. struct perf_event *group_leader,
  6121. struct perf_event *parent_event,
  6122. perf_overflow_handler_t overflow_handler,
  6123. void *context, int cgroup_fd)
  6124. {
  6125. struct pmu *pmu;
  6126. struct perf_event *event;
  6127. struct hw_perf_event *hwc;
  6128. long err = -EINVAL;
  6129. if ((unsigned)cpu >= nr_cpu_ids) {
  6130. if (!task || cpu != -1)
  6131. return ERR_PTR(-EINVAL);
  6132. }
  6133. event = kzalloc(sizeof(*event), GFP_KERNEL);
  6134. if (!event)
  6135. return ERR_PTR(-ENOMEM);
  6136. /*
  6137. * Single events are their own group leaders, with an
  6138. * empty sibling list:
  6139. */
  6140. if (!group_leader)
  6141. group_leader = event;
  6142. mutex_init(&event->child_mutex);
  6143. INIT_LIST_HEAD(&event->child_list);
  6144. INIT_LIST_HEAD(&event->group_entry);
  6145. INIT_LIST_HEAD(&event->event_entry);
  6146. INIT_LIST_HEAD(&event->sibling_list);
  6147. INIT_LIST_HEAD(&event->rb_entry);
  6148. INIT_LIST_HEAD(&event->active_entry);
  6149. INIT_HLIST_NODE(&event->hlist_entry);
  6150. init_waitqueue_head(&event->waitq);
  6151. init_irq_work(&event->pending, perf_pending_event);
  6152. mutex_init(&event->mmap_mutex);
  6153. atomic_long_set(&event->refcount, 1);
  6154. event->cpu = cpu;
  6155. event->attr = *attr;
  6156. event->group_leader = group_leader;
  6157. event->pmu = NULL;
  6158. event->oncpu = -1;
  6159. event->parent = parent_event;
  6160. event->ns = get_pid_ns(task_active_pid_ns(current));
  6161. event->id = atomic64_inc_return(&perf_event_id);
  6162. event->state = PERF_EVENT_STATE_INACTIVE;
  6163. if (task) {
  6164. event->attach_state = PERF_ATTACH_TASK;
  6165. /*
  6166. * XXX pmu::event_init needs to know what task to account to
  6167. * and we cannot use the ctx information because we need the
  6168. * pmu before we get a ctx.
  6169. */
  6170. event->hw.target = task;
  6171. }
  6172. event->clock = &local_clock;
  6173. if (parent_event)
  6174. event->clock = parent_event->clock;
  6175. if (!overflow_handler && parent_event) {
  6176. overflow_handler = parent_event->overflow_handler;
  6177. context = parent_event->overflow_handler_context;
  6178. }
  6179. event->overflow_handler = overflow_handler;
  6180. event->overflow_handler_context = context;
  6181. perf_event__state_init(event);
  6182. pmu = NULL;
  6183. hwc = &event->hw;
  6184. hwc->sample_period = attr->sample_period;
  6185. if (attr->freq && attr->sample_freq)
  6186. hwc->sample_period = 1;
  6187. hwc->last_period = hwc->sample_period;
  6188. local64_set(&hwc->period_left, hwc->sample_period);
  6189. /*
  6190. * we currently do not support PERF_FORMAT_GROUP on inherited events
  6191. */
  6192. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  6193. goto err_ns;
  6194. if (!has_branch_stack(event))
  6195. event->attr.branch_sample_type = 0;
  6196. if (cgroup_fd != -1) {
  6197. err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
  6198. if (err)
  6199. goto err_ns;
  6200. }
  6201. pmu = perf_init_event(event);
  6202. if (!pmu)
  6203. goto err_ns;
  6204. else if (IS_ERR(pmu)) {
  6205. err = PTR_ERR(pmu);
  6206. goto err_ns;
  6207. }
  6208. err = exclusive_event_init(event);
  6209. if (err)
  6210. goto err_pmu;
  6211. if (!event->parent) {
  6212. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  6213. err = get_callchain_buffers();
  6214. if (err)
  6215. goto err_per_task;
  6216. }
  6217. }
  6218. return event;
  6219. err_per_task:
  6220. exclusive_event_destroy(event);
  6221. err_pmu:
  6222. if (event->destroy)
  6223. event->destroy(event);
  6224. module_put(pmu->module);
  6225. err_ns:
  6226. if (is_cgroup_event(event))
  6227. perf_detach_cgroup(event);
  6228. if (event->ns)
  6229. put_pid_ns(event->ns);
  6230. kfree(event);
  6231. return ERR_PTR(err);
  6232. }
  6233. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  6234. struct perf_event_attr *attr)
  6235. {
  6236. u32 size;
  6237. int ret;
  6238. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  6239. return -EFAULT;
  6240. /*
  6241. * zero the full structure, so that a short copy will be nice.
  6242. */
  6243. memset(attr, 0, sizeof(*attr));
  6244. ret = get_user(size, &uattr->size);
  6245. if (ret)
  6246. return ret;
  6247. if (size > PAGE_SIZE) /* silly large */
  6248. goto err_size;
  6249. if (!size) /* abi compat */
  6250. size = PERF_ATTR_SIZE_VER0;
  6251. if (size < PERF_ATTR_SIZE_VER0)
  6252. goto err_size;
  6253. /*
  6254. * If we're handed a bigger struct than we know of,
  6255. * ensure all the unknown bits are 0 - i.e. new
  6256. * user-space does not rely on any kernel feature
  6257. * extensions we dont know about yet.
  6258. */
  6259. if (size > sizeof(*attr)) {
  6260. unsigned char __user *addr;
  6261. unsigned char __user *end;
  6262. unsigned char val;
  6263. addr = (void __user *)uattr + sizeof(*attr);
  6264. end = (void __user *)uattr + size;
  6265. for (; addr < end; addr++) {
  6266. ret = get_user(val, addr);
  6267. if (ret)
  6268. return ret;
  6269. if (val)
  6270. goto err_size;
  6271. }
  6272. size = sizeof(*attr);
  6273. }
  6274. ret = copy_from_user(attr, uattr, size);
  6275. if (ret)
  6276. return -EFAULT;
  6277. if (attr->__reserved_1)
  6278. return -EINVAL;
  6279. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  6280. return -EINVAL;
  6281. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  6282. return -EINVAL;
  6283. if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
  6284. u64 mask = attr->branch_sample_type;
  6285. /* only using defined bits */
  6286. if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
  6287. return -EINVAL;
  6288. /* at least one branch bit must be set */
  6289. if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
  6290. return -EINVAL;
  6291. /* propagate priv level, when not set for branch */
  6292. if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
  6293. /* exclude_kernel checked on syscall entry */
  6294. if (!attr->exclude_kernel)
  6295. mask |= PERF_SAMPLE_BRANCH_KERNEL;
  6296. if (!attr->exclude_user)
  6297. mask |= PERF_SAMPLE_BRANCH_USER;
  6298. if (!attr->exclude_hv)
  6299. mask |= PERF_SAMPLE_BRANCH_HV;
  6300. /*
  6301. * adjust user setting (for HW filter setup)
  6302. */
  6303. attr->branch_sample_type = mask;
  6304. }
  6305. /* privileged levels capture (kernel, hv): check permissions */
  6306. if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
  6307. && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  6308. return -EACCES;
  6309. }
  6310. if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
  6311. ret = perf_reg_validate(attr->sample_regs_user);
  6312. if (ret)
  6313. return ret;
  6314. }
  6315. if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
  6316. if (!arch_perf_have_user_stack_dump())
  6317. return -ENOSYS;
  6318. /*
  6319. * We have __u32 type for the size, but so far
  6320. * we can only use __u16 as maximum due to the
  6321. * __u16 sample size limit.
  6322. */
  6323. if (attr->sample_stack_user >= USHRT_MAX)
  6324. ret = -EINVAL;
  6325. else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
  6326. ret = -EINVAL;
  6327. }
  6328. if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
  6329. ret = perf_reg_validate(attr->sample_regs_intr);
  6330. out:
  6331. return ret;
  6332. err_size:
  6333. put_user(sizeof(*attr), &uattr->size);
  6334. ret = -E2BIG;
  6335. goto out;
  6336. }
  6337. static int
  6338. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  6339. {
  6340. struct ring_buffer *rb = NULL;
  6341. int ret = -EINVAL;
  6342. if (!output_event)
  6343. goto set;
  6344. /* don't allow circular references */
  6345. if (event == output_event)
  6346. goto out;
  6347. /*
  6348. * Don't allow cross-cpu buffers
  6349. */
  6350. if (output_event->cpu != event->cpu)
  6351. goto out;
  6352. /*
  6353. * If its not a per-cpu rb, it must be the same task.
  6354. */
  6355. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  6356. goto out;
  6357. /*
  6358. * Mixing clocks in the same buffer is trouble you don't need.
  6359. */
  6360. if (output_event->clock != event->clock)
  6361. goto out;
  6362. /*
  6363. * If both events generate aux data, they must be on the same PMU
  6364. */
  6365. if (has_aux(event) && has_aux(output_event) &&
  6366. event->pmu != output_event->pmu)
  6367. goto out;
  6368. set:
  6369. mutex_lock(&event->mmap_mutex);
  6370. /* Can't redirect output if we've got an active mmap() */
  6371. if (atomic_read(&event->mmap_count))
  6372. goto unlock;
  6373. if (output_event) {
  6374. /* get the rb we want to redirect to */
  6375. rb = ring_buffer_get(output_event);
  6376. if (!rb)
  6377. goto unlock;
  6378. }
  6379. ring_buffer_attach(event, rb);
  6380. ret = 0;
  6381. unlock:
  6382. mutex_unlock(&event->mmap_mutex);
  6383. out:
  6384. return ret;
  6385. }
  6386. static void mutex_lock_double(struct mutex *a, struct mutex *b)
  6387. {
  6388. if (b < a)
  6389. swap(a, b);
  6390. mutex_lock(a);
  6391. mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
  6392. }
  6393. static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
  6394. {
  6395. bool nmi_safe = false;
  6396. switch (clk_id) {
  6397. case CLOCK_MONOTONIC:
  6398. event->clock = &ktime_get_mono_fast_ns;
  6399. nmi_safe = true;
  6400. break;
  6401. case CLOCK_MONOTONIC_RAW:
  6402. event->clock = &ktime_get_raw_fast_ns;
  6403. nmi_safe = true;
  6404. break;
  6405. case CLOCK_REALTIME:
  6406. event->clock = &ktime_get_real_ns;
  6407. break;
  6408. case CLOCK_BOOTTIME:
  6409. event->clock = &ktime_get_boot_ns;
  6410. break;
  6411. case CLOCK_TAI:
  6412. event->clock = &ktime_get_tai_ns;
  6413. break;
  6414. default:
  6415. return -EINVAL;
  6416. }
  6417. if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
  6418. return -EINVAL;
  6419. return 0;
  6420. }
  6421. /**
  6422. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  6423. *
  6424. * @attr_uptr: event_id type attributes for monitoring/sampling
  6425. * @pid: target pid
  6426. * @cpu: target cpu
  6427. * @group_fd: group leader event fd
  6428. */
  6429. SYSCALL_DEFINE5(perf_event_open,
  6430. struct perf_event_attr __user *, attr_uptr,
  6431. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  6432. {
  6433. struct perf_event *group_leader = NULL, *output_event = NULL;
  6434. struct perf_event *event, *sibling;
  6435. struct perf_event_attr attr;
  6436. struct perf_event_context *ctx, *uninitialized_var(gctx);
  6437. struct file *event_file = NULL;
  6438. struct fd group = {NULL, 0};
  6439. struct task_struct *task = NULL;
  6440. struct pmu *pmu;
  6441. int event_fd;
  6442. int move_group = 0;
  6443. int err;
  6444. int f_flags = O_RDWR;
  6445. int cgroup_fd = -1;
  6446. /* for future expandability... */
  6447. if (flags & ~PERF_FLAG_ALL)
  6448. return -EINVAL;
  6449. err = perf_copy_attr(attr_uptr, &attr);
  6450. if (err)
  6451. return err;
  6452. if (!attr.exclude_kernel) {
  6453. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  6454. return -EACCES;
  6455. }
  6456. if (attr.freq) {
  6457. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  6458. return -EINVAL;
  6459. } else {
  6460. if (attr.sample_period & (1ULL << 63))
  6461. return -EINVAL;
  6462. }
  6463. /*
  6464. * In cgroup mode, the pid argument is used to pass the fd
  6465. * opened to the cgroup directory in cgroupfs. The cpu argument
  6466. * designates the cpu on which to monitor threads from that
  6467. * cgroup.
  6468. */
  6469. if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
  6470. return -EINVAL;
  6471. if (flags & PERF_FLAG_FD_CLOEXEC)
  6472. f_flags |= O_CLOEXEC;
  6473. event_fd = get_unused_fd_flags(f_flags);
  6474. if (event_fd < 0)
  6475. return event_fd;
  6476. if (group_fd != -1) {
  6477. err = perf_fget_light(group_fd, &group);
  6478. if (err)
  6479. goto err_fd;
  6480. group_leader = group.file->private_data;
  6481. if (flags & PERF_FLAG_FD_OUTPUT)
  6482. output_event = group_leader;
  6483. if (flags & PERF_FLAG_FD_NO_GROUP)
  6484. group_leader = NULL;
  6485. }
  6486. if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
  6487. task = find_lively_task_by_vpid(pid);
  6488. if (IS_ERR(task)) {
  6489. err = PTR_ERR(task);
  6490. goto err_group_fd;
  6491. }
  6492. }
  6493. if (task && group_leader &&
  6494. group_leader->attr.inherit != attr.inherit) {
  6495. err = -EINVAL;
  6496. goto err_task;
  6497. }
  6498. get_online_cpus();
  6499. if (flags & PERF_FLAG_PID_CGROUP)
  6500. cgroup_fd = pid;
  6501. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
  6502. NULL, NULL, cgroup_fd);
  6503. if (IS_ERR(event)) {
  6504. err = PTR_ERR(event);
  6505. goto err_cpus;
  6506. }
  6507. if (is_sampling_event(event)) {
  6508. if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
  6509. err = -ENOTSUPP;
  6510. goto err_alloc;
  6511. }
  6512. }
  6513. account_event(event);
  6514. /*
  6515. * Special case software events and allow them to be part of
  6516. * any hardware group.
  6517. */
  6518. pmu = event->pmu;
  6519. if (attr.use_clockid) {
  6520. err = perf_event_set_clock(event, attr.clockid);
  6521. if (err)
  6522. goto err_alloc;
  6523. }
  6524. if (group_leader &&
  6525. (is_software_event(event) != is_software_event(group_leader))) {
  6526. if (is_software_event(event)) {
  6527. /*
  6528. * If event and group_leader are not both a software
  6529. * event, and event is, then group leader is not.
  6530. *
  6531. * Allow the addition of software events to !software
  6532. * groups, this is safe because software events never
  6533. * fail to schedule.
  6534. */
  6535. pmu = group_leader->pmu;
  6536. } else if (is_software_event(group_leader) &&
  6537. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  6538. /*
  6539. * In case the group is a pure software group, and we
  6540. * try to add a hardware event, move the whole group to
  6541. * the hardware context.
  6542. */
  6543. move_group = 1;
  6544. }
  6545. }
  6546. /*
  6547. * Get the target context (task or percpu):
  6548. */
  6549. ctx = find_get_context(pmu, task, event);
  6550. if (IS_ERR(ctx)) {
  6551. err = PTR_ERR(ctx);
  6552. goto err_alloc;
  6553. }
  6554. if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
  6555. err = -EBUSY;
  6556. goto err_context;
  6557. }
  6558. if (task) {
  6559. put_task_struct(task);
  6560. task = NULL;
  6561. }
  6562. /*
  6563. * Look up the group leader (we will attach this event to it):
  6564. */
  6565. if (group_leader) {
  6566. err = -EINVAL;
  6567. /*
  6568. * Do not allow a recursive hierarchy (this new sibling
  6569. * becoming part of another group-sibling):
  6570. */
  6571. if (group_leader->group_leader != group_leader)
  6572. goto err_context;
  6573. /* All events in a group should have the same clock */
  6574. if (group_leader->clock != event->clock)
  6575. goto err_context;
  6576. /*
  6577. * Do not allow to attach to a group in a different
  6578. * task or CPU context:
  6579. */
  6580. if (move_group) {
  6581. /*
  6582. * Make sure we're both on the same task, or both
  6583. * per-cpu events.
  6584. */
  6585. if (group_leader->ctx->task != ctx->task)
  6586. goto err_context;
  6587. /*
  6588. * Make sure we're both events for the same CPU;
  6589. * grouping events for different CPUs is broken; since
  6590. * you can never concurrently schedule them anyhow.
  6591. */
  6592. if (group_leader->cpu != event->cpu)
  6593. goto err_context;
  6594. } else {
  6595. if (group_leader->ctx != ctx)
  6596. goto err_context;
  6597. }
  6598. /*
  6599. * Only a group leader can be exclusive or pinned
  6600. */
  6601. if (attr.exclusive || attr.pinned)
  6602. goto err_context;
  6603. }
  6604. if (output_event) {
  6605. err = perf_event_set_output(event, output_event);
  6606. if (err)
  6607. goto err_context;
  6608. }
  6609. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
  6610. f_flags);
  6611. if (IS_ERR(event_file)) {
  6612. err = PTR_ERR(event_file);
  6613. goto err_context;
  6614. }
  6615. if (move_group) {
  6616. gctx = group_leader->ctx;
  6617. /*
  6618. * See perf_event_ctx_lock() for comments on the details
  6619. * of swizzling perf_event::ctx.
  6620. */
  6621. mutex_lock_double(&gctx->mutex, &ctx->mutex);
  6622. perf_remove_from_context(group_leader, false);
  6623. list_for_each_entry(sibling, &group_leader->sibling_list,
  6624. group_entry) {
  6625. perf_remove_from_context(sibling, false);
  6626. put_ctx(gctx);
  6627. }
  6628. } else {
  6629. mutex_lock(&ctx->mutex);
  6630. }
  6631. WARN_ON_ONCE(ctx->parent_ctx);
  6632. if (move_group) {
  6633. /*
  6634. * Wait for everybody to stop referencing the events through
  6635. * the old lists, before installing it on new lists.
  6636. */
  6637. synchronize_rcu();
  6638. /*
  6639. * Install the group siblings before the group leader.
  6640. *
  6641. * Because a group leader will try and install the entire group
  6642. * (through the sibling list, which is still in-tact), we can
  6643. * end up with siblings installed in the wrong context.
  6644. *
  6645. * By installing siblings first we NO-OP because they're not
  6646. * reachable through the group lists.
  6647. */
  6648. list_for_each_entry(sibling, &group_leader->sibling_list,
  6649. group_entry) {
  6650. perf_event__state_init(sibling);
  6651. perf_install_in_context(ctx, sibling, sibling->cpu);
  6652. get_ctx(ctx);
  6653. }
  6654. /*
  6655. * Removing from the context ends up with disabled
  6656. * event. What we want here is event in the initial
  6657. * startup state, ready to be add into new context.
  6658. */
  6659. perf_event__state_init(group_leader);
  6660. perf_install_in_context(ctx, group_leader, group_leader->cpu);
  6661. get_ctx(ctx);
  6662. }
  6663. if (!exclusive_event_installable(event, ctx)) {
  6664. err = -EBUSY;
  6665. mutex_unlock(&ctx->mutex);
  6666. fput(event_file);
  6667. goto err_context;
  6668. }
  6669. perf_install_in_context(ctx, event, event->cpu);
  6670. perf_unpin_context(ctx);
  6671. if (move_group) {
  6672. mutex_unlock(&gctx->mutex);
  6673. put_ctx(gctx);
  6674. }
  6675. mutex_unlock(&ctx->mutex);
  6676. put_online_cpus();
  6677. event->owner = current;
  6678. mutex_lock(&current->perf_event_mutex);
  6679. list_add_tail(&event->owner_entry, &current->perf_event_list);
  6680. mutex_unlock(&current->perf_event_mutex);
  6681. /*
  6682. * Precalculate sample_data sizes
  6683. */
  6684. perf_event__header_size(event);
  6685. perf_event__id_header_size(event);
  6686. /*
  6687. * Drop the reference on the group_event after placing the
  6688. * new event on the sibling_list. This ensures destruction
  6689. * of the group leader will find the pointer to itself in
  6690. * perf_group_detach().
  6691. */
  6692. fdput(group);
  6693. fd_install(event_fd, event_file);
  6694. return event_fd;
  6695. err_context:
  6696. perf_unpin_context(ctx);
  6697. put_ctx(ctx);
  6698. err_alloc:
  6699. free_event(event);
  6700. err_cpus:
  6701. put_online_cpus();
  6702. err_task:
  6703. if (task)
  6704. put_task_struct(task);
  6705. err_group_fd:
  6706. fdput(group);
  6707. err_fd:
  6708. put_unused_fd(event_fd);
  6709. return err;
  6710. }
  6711. /**
  6712. * perf_event_create_kernel_counter
  6713. *
  6714. * @attr: attributes of the counter to create
  6715. * @cpu: cpu in which the counter is bound
  6716. * @task: task to profile (NULL for percpu)
  6717. */
  6718. struct perf_event *
  6719. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  6720. struct task_struct *task,
  6721. perf_overflow_handler_t overflow_handler,
  6722. void *context)
  6723. {
  6724. struct perf_event_context *ctx;
  6725. struct perf_event *event;
  6726. int err;
  6727. /*
  6728. * Get the target context (task or percpu):
  6729. */
  6730. event = perf_event_alloc(attr, cpu, task, NULL, NULL,
  6731. overflow_handler, context, -1);
  6732. if (IS_ERR(event)) {
  6733. err = PTR_ERR(event);
  6734. goto err;
  6735. }
  6736. /* Mark owner so we could distinguish it from user events. */
  6737. event->owner = EVENT_OWNER_KERNEL;
  6738. account_event(event);
  6739. ctx = find_get_context(event->pmu, task, event);
  6740. if (IS_ERR(ctx)) {
  6741. err = PTR_ERR(ctx);
  6742. goto err_free;
  6743. }
  6744. WARN_ON_ONCE(ctx->parent_ctx);
  6745. mutex_lock(&ctx->mutex);
  6746. if (!exclusive_event_installable(event, ctx)) {
  6747. mutex_unlock(&ctx->mutex);
  6748. perf_unpin_context(ctx);
  6749. put_ctx(ctx);
  6750. err = -EBUSY;
  6751. goto err_free;
  6752. }
  6753. perf_install_in_context(ctx, event, cpu);
  6754. perf_unpin_context(ctx);
  6755. mutex_unlock(&ctx->mutex);
  6756. return event;
  6757. err_free:
  6758. free_event(event);
  6759. err:
  6760. return ERR_PTR(err);
  6761. }
  6762. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  6763. void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
  6764. {
  6765. struct perf_event_context *src_ctx;
  6766. struct perf_event_context *dst_ctx;
  6767. struct perf_event *event, *tmp;
  6768. LIST_HEAD(events);
  6769. src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
  6770. dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
  6771. /*
  6772. * See perf_event_ctx_lock() for comments on the details
  6773. * of swizzling perf_event::ctx.
  6774. */
  6775. mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
  6776. list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
  6777. event_entry) {
  6778. perf_remove_from_context(event, false);
  6779. unaccount_event_cpu(event, src_cpu);
  6780. put_ctx(src_ctx);
  6781. list_add(&event->migrate_entry, &events);
  6782. }
  6783. /*
  6784. * Wait for the events to quiesce before re-instating them.
  6785. */
  6786. synchronize_rcu();
  6787. /*
  6788. * Re-instate events in 2 passes.
  6789. *
  6790. * Skip over group leaders and only install siblings on this first
  6791. * pass, siblings will not get enabled without a leader, however a
  6792. * leader will enable its siblings, even if those are still on the old
  6793. * context.
  6794. */
  6795. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  6796. if (event->group_leader == event)
  6797. continue;
  6798. list_del(&event->migrate_entry);
  6799. if (event->state >= PERF_EVENT_STATE_OFF)
  6800. event->state = PERF_EVENT_STATE_INACTIVE;
  6801. account_event_cpu(event, dst_cpu);
  6802. perf_install_in_context(dst_ctx, event, dst_cpu);
  6803. get_ctx(dst_ctx);
  6804. }
  6805. /*
  6806. * Once all the siblings are setup properly, install the group leaders
  6807. * to make it go.
  6808. */
  6809. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  6810. list_del(&event->migrate_entry);
  6811. if (event->state >= PERF_EVENT_STATE_OFF)
  6812. event->state = PERF_EVENT_STATE_INACTIVE;
  6813. account_event_cpu(event, dst_cpu);
  6814. perf_install_in_context(dst_ctx, event, dst_cpu);
  6815. get_ctx(dst_ctx);
  6816. }
  6817. mutex_unlock(&dst_ctx->mutex);
  6818. mutex_unlock(&src_ctx->mutex);
  6819. }
  6820. EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
  6821. static void sync_child_event(struct perf_event *child_event,
  6822. struct task_struct *child)
  6823. {
  6824. struct perf_event *parent_event = child_event->parent;
  6825. u64 child_val;
  6826. if (child_event->attr.inherit_stat)
  6827. perf_event_read_event(child_event, child);
  6828. child_val = perf_event_count(child_event);
  6829. /*
  6830. * Add back the child's count to the parent's count:
  6831. */
  6832. atomic64_add(child_val, &parent_event->child_count);
  6833. atomic64_add(child_event->total_time_enabled,
  6834. &parent_event->child_total_time_enabled);
  6835. atomic64_add(child_event->total_time_running,
  6836. &parent_event->child_total_time_running);
  6837. /*
  6838. * Remove this event from the parent's list
  6839. */
  6840. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  6841. mutex_lock(&parent_event->child_mutex);
  6842. list_del_init(&child_event->child_list);
  6843. mutex_unlock(&parent_event->child_mutex);
  6844. /*
  6845. * Make sure user/parent get notified, that we just
  6846. * lost one event.
  6847. */
  6848. perf_event_wakeup(parent_event);
  6849. /*
  6850. * Release the parent event, if this was the last
  6851. * reference to it.
  6852. */
  6853. put_event(parent_event);
  6854. }
  6855. static void
  6856. __perf_event_exit_task(struct perf_event *child_event,
  6857. struct perf_event_context *child_ctx,
  6858. struct task_struct *child)
  6859. {
  6860. /*
  6861. * Do not destroy the 'original' grouping; because of the context
  6862. * switch optimization the original events could've ended up in a
  6863. * random child task.
  6864. *
  6865. * If we were to destroy the original group, all group related
  6866. * operations would cease to function properly after this random
  6867. * child dies.
  6868. *
  6869. * Do destroy all inherited groups, we don't care about those
  6870. * and being thorough is better.
  6871. */
  6872. perf_remove_from_context(child_event, !!child_event->parent);
  6873. /*
  6874. * It can happen that the parent exits first, and has events
  6875. * that are still around due to the child reference. These
  6876. * events need to be zapped.
  6877. */
  6878. if (child_event->parent) {
  6879. sync_child_event(child_event, child);
  6880. free_event(child_event);
  6881. } else {
  6882. child_event->state = PERF_EVENT_STATE_EXIT;
  6883. perf_event_wakeup(child_event);
  6884. }
  6885. }
  6886. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  6887. {
  6888. struct perf_event *child_event, *next;
  6889. struct perf_event_context *child_ctx, *clone_ctx = NULL;
  6890. unsigned long flags;
  6891. if (likely(!child->perf_event_ctxp[ctxn])) {
  6892. perf_event_task(child, NULL, 0);
  6893. return;
  6894. }
  6895. local_irq_save(flags);
  6896. /*
  6897. * We can't reschedule here because interrupts are disabled,
  6898. * and either child is current or it is a task that can't be
  6899. * scheduled, so we are now safe from rescheduling changing
  6900. * our context.
  6901. */
  6902. child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
  6903. /*
  6904. * Take the context lock here so that if find_get_context is
  6905. * reading child->perf_event_ctxp, we wait until it has
  6906. * incremented the context's refcount before we do put_ctx below.
  6907. */
  6908. raw_spin_lock(&child_ctx->lock);
  6909. task_ctx_sched_out(child_ctx);
  6910. child->perf_event_ctxp[ctxn] = NULL;
  6911. /*
  6912. * If this context is a clone; unclone it so it can't get
  6913. * swapped to another process while we're removing all
  6914. * the events from it.
  6915. */
  6916. clone_ctx = unclone_ctx(child_ctx);
  6917. update_context_time(child_ctx);
  6918. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  6919. if (clone_ctx)
  6920. put_ctx(clone_ctx);
  6921. /*
  6922. * Report the task dead after unscheduling the events so that we
  6923. * won't get any samples after PERF_RECORD_EXIT. We can however still
  6924. * get a few PERF_RECORD_READ events.
  6925. */
  6926. perf_event_task(child, child_ctx, 0);
  6927. /*
  6928. * We can recurse on the same lock type through:
  6929. *
  6930. * __perf_event_exit_task()
  6931. * sync_child_event()
  6932. * put_event()
  6933. * mutex_lock(&ctx->mutex)
  6934. *
  6935. * But since its the parent context it won't be the same instance.
  6936. */
  6937. mutex_lock(&child_ctx->mutex);
  6938. list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
  6939. __perf_event_exit_task(child_event, child_ctx, child);
  6940. mutex_unlock(&child_ctx->mutex);
  6941. put_ctx(child_ctx);
  6942. }
  6943. /*
  6944. * When a child task exits, feed back event values to parent events.
  6945. */
  6946. void perf_event_exit_task(struct task_struct *child)
  6947. {
  6948. struct perf_event *event, *tmp;
  6949. int ctxn;
  6950. mutex_lock(&child->perf_event_mutex);
  6951. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  6952. owner_entry) {
  6953. list_del_init(&event->owner_entry);
  6954. /*
  6955. * Ensure the list deletion is visible before we clear
  6956. * the owner, closes a race against perf_release() where
  6957. * we need to serialize on the owner->perf_event_mutex.
  6958. */
  6959. smp_wmb();
  6960. event->owner = NULL;
  6961. }
  6962. mutex_unlock(&child->perf_event_mutex);
  6963. for_each_task_context_nr(ctxn)
  6964. perf_event_exit_task_context(child, ctxn);
  6965. }
  6966. static void perf_free_event(struct perf_event *event,
  6967. struct perf_event_context *ctx)
  6968. {
  6969. struct perf_event *parent = event->parent;
  6970. if (WARN_ON_ONCE(!parent))
  6971. return;
  6972. mutex_lock(&parent->child_mutex);
  6973. list_del_init(&event->child_list);
  6974. mutex_unlock(&parent->child_mutex);
  6975. put_event(parent);
  6976. raw_spin_lock_irq(&ctx->lock);
  6977. perf_group_detach(event);
  6978. list_del_event(event, ctx);
  6979. raw_spin_unlock_irq(&ctx->lock);
  6980. free_event(event);
  6981. }
  6982. /*
  6983. * Free an unexposed, unused context as created by inheritance by
  6984. * perf_event_init_task below, used by fork() in case of fail.
  6985. *
  6986. * Not all locks are strictly required, but take them anyway to be nice and
  6987. * help out with the lockdep assertions.
  6988. */
  6989. void perf_event_free_task(struct task_struct *task)
  6990. {
  6991. struct perf_event_context *ctx;
  6992. struct perf_event *event, *tmp;
  6993. int ctxn;
  6994. for_each_task_context_nr(ctxn) {
  6995. ctx = task->perf_event_ctxp[ctxn];
  6996. if (!ctx)
  6997. continue;
  6998. mutex_lock(&ctx->mutex);
  6999. again:
  7000. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  7001. group_entry)
  7002. perf_free_event(event, ctx);
  7003. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  7004. group_entry)
  7005. perf_free_event(event, ctx);
  7006. if (!list_empty(&ctx->pinned_groups) ||
  7007. !list_empty(&ctx->flexible_groups))
  7008. goto again;
  7009. mutex_unlock(&ctx->mutex);
  7010. put_ctx(ctx);
  7011. }
  7012. }
  7013. void perf_event_delayed_put(struct task_struct *task)
  7014. {
  7015. int ctxn;
  7016. for_each_task_context_nr(ctxn)
  7017. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  7018. }
  7019. /*
  7020. * inherit a event from parent task to child task:
  7021. */
  7022. static struct perf_event *
  7023. inherit_event(struct perf_event *parent_event,
  7024. struct task_struct *parent,
  7025. struct perf_event_context *parent_ctx,
  7026. struct task_struct *child,
  7027. struct perf_event *group_leader,
  7028. struct perf_event_context *child_ctx)
  7029. {
  7030. enum perf_event_active_state parent_state = parent_event->state;
  7031. struct perf_event *child_event;
  7032. unsigned long flags;
  7033. /*
  7034. * Instead of creating recursive hierarchies of events,
  7035. * we link inherited events back to the original parent,
  7036. * which has a filp for sure, which we use as the reference
  7037. * count:
  7038. */
  7039. if (parent_event->parent)
  7040. parent_event = parent_event->parent;
  7041. child_event = perf_event_alloc(&parent_event->attr,
  7042. parent_event->cpu,
  7043. child,
  7044. group_leader, parent_event,
  7045. NULL, NULL, -1);
  7046. if (IS_ERR(child_event))
  7047. return child_event;
  7048. if (is_orphaned_event(parent_event) ||
  7049. !atomic_long_inc_not_zero(&parent_event->refcount)) {
  7050. free_event(child_event);
  7051. return NULL;
  7052. }
  7053. get_ctx(child_ctx);
  7054. /*
  7055. * Make the child state follow the state of the parent event,
  7056. * not its attr.disabled bit. We hold the parent's mutex,
  7057. * so we won't race with perf_event_{en, dis}able_family.
  7058. */
  7059. if (parent_state >= PERF_EVENT_STATE_INACTIVE)
  7060. child_event->state = PERF_EVENT_STATE_INACTIVE;
  7061. else
  7062. child_event->state = PERF_EVENT_STATE_OFF;
  7063. if (parent_event->attr.freq) {
  7064. u64 sample_period = parent_event->hw.sample_period;
  7065. struct hw_perf_event *hwc = &child_event->hw;
  7066. hwc->sample_period = sample_period;
  7067. hwc->last_period = sample_period;
  7068. local64_set(&hwc->period_left, sample_period);
  7069. }
  7070. child_event->ctx = child_ctx;
  7071. child_event->overflow_handler = parent_event->overflow_handler;
  7072. child_event->overflow_handler_context
  7073. = parent_event->overflow_handler_context;
  7074. /*
  7075. * Precalculate sample_data sizes
  7076. */
  7077. perf_event__header_size(child_event);
  7078. perf_event__id_header_size(child_event);
  7079. /*
  7080. * Link it up in the child's context:
  7081. */
  7082. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  7083. add_event_to_ctx(child_event, child_ctx);
  7084. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  7085. /*
  7086. * Link this into the parent event's child list
  7087. */
  7088. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  7089. mutex_lock(&parent_event->child_mutex);
  7090. list_add_tail(&child_event->child_list, &parent_event->child_list);
  7091. mutex_unlock(&parent_event->child_mutex);
  7092. return child_event;
  7093. }
  7094. static int inherit_group(struct perf_event *parent_event,
  7095. struct task_struct *parent,
  7096. struct perf_event_context *parent_ctx,
  7097. struct task_struct *child,
  7098. struct perf_event_context *child_ctx)
  7099. {
  7100. struct perf_event *leader;
  7101. struct perf_event *sub;
  7102. struct perf_event *child_ctr;
  7103. leader = inherit_event(parent_event, parent, parent_ctx,
  7104. child, NULL, child_ctx);
  7105. if (IS_ERR(leader))
  7106. return PTR_ERR(leader);
  7107. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  7108. child_ctr = inherit_event(sub, parent, parent_ctx,
  7109. child, leader, child_ctx);
  7110. if (IS_ERR(child_ctr))
  7111. return PTR_ERR(child_ctr);
  7112. }
  7113. return 0;
  7114. }
  7115. static int
  7116. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  7117. struct perf_event_context *parent_ctx,
  7118. struct task_struct *child, int ctxn,
  7119. int *inherited_all)
  7120. {
  7121. int ret;
  7122. struct perf_event_context *child_ctx;
  7123. if (!event->attr.inherit) {
  7124. *inherited_all = 0;
  7125. return 0;
  7126. }
  7127. child_ctx = child->perf_event_ctxp[ctxn];
  7128. if (!child_ctx) {
  7129. /*
  7130. * This is executed from the parent task context, so
  7131. * inherit events that have been marked for cloning.
  7132. * First allocate and initialize a context for the
  7133. * child.
  7134. */
  7135. child_ctx = alloc_perf_context(parent_ctx->pmu, child);
  7136. if (!child_ctx)
  7137. return -ENOMEM;
  7138. child->perf_event_ctxp[ctxn] = child_ctx;
  7139. }
  7140. ret = inherit_group(event, parent, parent_ctx,
  7141. child, child_ctx);
  7142. if (ret)
  7143. *inherited_all = 0;
  7144. return ret;
  7145. }
  7146. /*
  7147. * Initialize the perf_event context in task_struct
  7148. */
  7149. static int perf_event_init_context(struct task_struct *child, int ctxn)
  7150. {
  7151. struct perf_event_context *child_ctx, *parent_ctx;
  7152. struct perf_event_context *cloned_ctx;
  7153. struct perf_event *event;
  7154. struct task_struct *parent = current;
  7155. int inherited_all = 1;
  7156. unsigned long flags;
  7157. int ret = 0;
  7158. if (likely(!parent->perf_event_ctxp[ctxn]))
  7159. return 0;
  7160. /*
  7161. * If the parent's context is a clone, pin it so it won't get
  7162. * swapped under us.
  7163. */
  7164. parent_ctx = perf_pin_task_context(parent, ctxn);
  7165. if (!parent_ctx)
  7166. return 0;
  7167. /*
  7168. * No need to check if parent_ctx != NULL here; since we saw
  7169. * it non-NULL earlier, the only reason for it to become NULL
  7170. * is if we exit, and since we're currently in the middle of
  7171. * a fork we can't be exiting at the same time.
  7172. */
  7173. /*
  7174. * Lock the parent list. No need to lock the child - not PID
  7175. * hashed yet and not running, so nobody can access it.
  7176. */
  7177. mutex_lock(&parent_ctx->mutex);
  7178. /*
  7179. * We dont have to disable NMIs - we are only looking at
  7180. * the list, not manipulating it:
  7181. */
  7182. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  7183. ret = inherit_task_group(event, parent, parent_ctx,
  7184. child, ctxn, &inherited_all);
  7185. if (ret)
  7186. break;
  7187. }
  7188. /*
  7189. * We can't hold ctx->lock when iterating the ->flexible_group list due
  7190. * to allocations, but we need to prevent rotation because
  7191. * rotate_ctx() will change the list from interrupt context.
  7192. */
  7193. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  7194. parent_ctx->rotate_disable = 1;
  7195. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  7196. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  7197. ret = inherit_task_group(event, parent, parent_ctx,
  7198. child, ctxn, &inherited_all);
  7199. if (ret)
  7200. break;
  7201. }
  7202. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  7203. parent_ctx->rotate_disable = 0;
  7204. child_ctx = child->perf_event_ctxp[ctxn];
  7205. if (child_ctx && inherited_all) {
  7206. /*
  7207. * Mark the child context as a clone of the parent
  7208. * context, or of whatever the parent is a clone of.
  7209. *
  7210. * Note that if the parent is a clone, the holding of
  7211. * parent_ctx->lock avoids it from being uncloned.
  7212. */
  7213. cloned_ctx = parent_ctx->parent_ctx;
  7214. if (cloned_ctx) {
  7215. child_ctx->parent_ctx = cloned_ctx;
  7216. child_ctx->parent_gen = parent_ctx->parent_gen;
  7217. } else {
  7218. child_ctx->parent_ctx = parent_ctx;
  7219. child_ctx->parent_gen = parent_ctx->generation;
  7220. }
  7221. get_ctx(child_ctx->parent_ctx);
  7222. }
  7223. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  7224. mutex_unlock(&parent_ctx->mutex);
  7225. perf_unpin_context(parent_ctx);
  7226. put_ctx(parent_ctx);
  7227. return ret;
  7228. }
  7229. /*
  7230. * Initialize the perf_event context in task_struct
  7231. */
  7232. int perf_event_init_task(struct task_struct *child)
  7233. {
  7234. int ctxn, ret;
  7235. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  7236. mutex_init(&child->perf_event_mutex);
  7237. INIT_LIST_HEAD(&child->perf_event_list);
  7238. for_each_task_context_nr(ctxn) {
  7239. ret = perf_event_init_context(child, ctxn);
  7240. if (ret) {
  7241. perf_event_free_task(child);
  7242. return ret;
  7243. }
  7244. }
  7245. return 0;
  7246. }
  7247. static void __init perf_event_init_all_cpus(void)
  7248. {
  7249. struct swevent_htable *swhash;
  7250. int cpu;
  7251. for_each_possible_cpu(cpu) {
  7252. swhash = &per_cpu(swevent_htable, cpu);
  7253. mutex_init(&swhash->hlist_mutex);
  7254. INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
  7255. }
  7256. }
  7257. static void perf_event_init_cpu(int cpu)
  7258. {
  7259. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  7260. mutex_lock(&swhash->hlist_mutex);
  7261. swhash->online = true;
  7262. if (swhash->hlist_refcount > 0) {
  7263. struct swevent_hlist *hlist;
  7264. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  7265. WARN_ON(!hlist);
  7266. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  7267. }
  7268. mutex_unlock(&swhash->hlist_mutex);
  7269. }
  7270. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
  7271. static void __perf_event_exit_context(void *__info)
  7272. {
  7273. struct remove_event re = { .detach_group = true };
  7274. struct perf_event_context *ctx = __info;
  7275. rcu_read_lock();
  7276. list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
  7277. __perf_remove_from_context(&re);
  7278. rcu_read_unlock();
  7279. }
  7280. static void perf_event_exit_cpu_context(int cpu)
  7281. {
  7282. struct perf_event_context *ctx;
  7283. struct pmu *pmu;
  7284. int idx;
  7285. idx = srcu_read_lock(&pmus_srcu);
  7286. list_for_each_entry_rcu(pmu, &pmus, entry) {
  7287. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  7288. mutex_lock(&ctx->mutex);
  7289. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  7290. mutex_unlock(&ctx->mutex);
  7291. }
  7292. srcu_read_unlock(&pmus_srcu, idx);
  7293. }
  7294. static void perf_event_exit_cpu(int cpu)
  7295. {
  7296. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  7297. perf_event_exit_cpu_context(cpu);
  7298. mutex_lock(&swhash->hlist_mutex);
  7299. swhash->online = false;
  7300. swevent_hlist_release(swhash);
  7301. mutex_unlock(&swhash->hlist_mutex);
  7302. }
  7303. #else
  7304. static inline void perf_event_exit_cpu(int cpu) { }
  7305. #endif
  7306. static int
  7307. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  7308. {
  7309. int cpu;
  7310. for_each_online_cpu(cpu)
  7311. perf_event_exit_cpu(cpu);
  7312. return NOTIFY_OK;
  7313. }
  7314. /*
  7315. * Run the perf reboot notifier at the very last possible moment so that
  7316. * the generic watchdog code runs as long as possible.
  7317. */
  7318. static struct notifier_block perf_reboot_notifier = {
  7319. .notifier_call = perf_reboot,
  7320. .priority = INT_MIN,
  7321. };
  7322. static int
  7323. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  7324. {
  7325. unsigned int cpu = (long)hcpu;
  7326. switch (action & ~CPU_TASKS_FROZEN) {
  7327. case CPU_UP_PREPARE:
  7328. case CPU_DOWN_FAILED:
  7329. perf_event_init_cpu(cpu);
  7330. break;
  7331. case CPU_UP_CANCELED:
  7332. case CPU_DOWN_PREPARE:
  7333. perf_event_exit_cpu(cpu);
  7334. break;
  7335. default:
  7336. break;
  7337. }
  7338. return NOTIFY_OK;
  7339. }
  7340. void __init perf_event_init(void)
  7341. {
  7342. int ret;
  7343. idr_init(&pmu_idr);
  7344. perf_event_init_all_cpus();
  7345. init_srcu_struct(&pmus_srcu);
  7346. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  7347. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  7348. perf_pmu_register(&perf_task_clock, NULL, -1);
  7349. perf_tp_register();
  7350. perf_cpu_notifier(perf_cpu_notify);
  7351. register_reboot_notifier(&perf_reboot_notifier);
  7352. ret = init_hw_breakpoint();
  7353. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  7354. /* do not patch jump label more than once per second */
  7355. jump_label_rate_limit(&perf_sched_events, HZ);
  7356. /*
  7357. * Build time assertion that we keep the data_head at the intended
  7358. * location. IOW, validation we got the __reserved[] size right.
  7359. */
  7360. BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
  7361. != 1024);
  7362. }
  7363. ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
  7364. char *page)
  7365. {
  7366. struct perf_pmu_events_attr *pmu_attr =
  7367. container_of(attr, struct perf_pmu_events_attr, attr);
  7368. if (pmu_attr->event_str)
  7369. return sprintf(page, "%s\n", pmu_attr->event_str);
  7370. return 0;
  7371. }
  7372. static int __init perf_event_sysfs_init(void)
  7373. {
  7374. struct pmu *pmu;
  7375. int ret;
  7376. mutex_lock(&pmus_lock);
  7377. ret = bus_register(&pmu_bus);
  7378. if (ret)
  7379. goto unlock;
  7380. list_for_each_entry(pmu, &pmus, entry) {
  7381. if (!pmu->name || pmu->type < 0)
  7382. continue;
  7383. ret = pmu_dev_alloc(pmu);
  7384. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  7385. }
  7386. pmu_bus_running = 1;
  7387. ret = 0;
  7388. unlock:
  7389. mutex_unlock(&pmus_lock);
  7390. return ret;
  7391. }
  7392. device_initcall(perf_event_sysfs_init);
  7393. #ifdef CONFIG_CGROUP_PERF
  7394. static struct cgroup_subsys_state *
  7395. perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
  7396. {
  7397. struct perf_cgroup *jc;
  7398. jc = kzalloc(sizeof(*jc), GFP_KERNEL);
  7399. if (!jc)
  7400. return ERR_PTR(-ENOMEM);
  7401. jc->info = alloc_percpu(struct perf_cgroup_info);
  7402. if (!jc->info) {
  7403. kfree(jc);
  7404. return ERR_PTR(-ENOMEM);
  7405. }
  7406. return &jc->css;
  7407. }
  7408. static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
  7409. {
  7410. struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
  7411. free_percpu(jc->info);
  7412. kfree(jc);
  7413. }
  7414. static int __perf_cgroup_move(void *info)
  7415. {
  7416. struct task_struct *task = info;
  7417. perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
  7418. return 0;
  7419. }
  7420. static void perf_cgroup_attach(struct cgroup_subsys_state *css,
  7421. struct cgroup_taskset *tset)
  7422. {
  7423. struct task_struct *task;
  7424. cgroup_taskset_for_each(task, tset)
  7425. task_function_call(task, __perf_cgroup_move, task);
  7426. }
  7427. static void perf_cgroup_exit(struct cgroup_subsys_state *css,
  7428. struct cgroup_subsys_state *old_css,
  7429. struct task_struct *task)
  7430. {
  7431. /*
  7432. * cgroup_exit() is called in the copy_process() failure path.
  7433. * Ignore this case since the task hasn't ran yet, this avoids
  7434. * trying to poke a half freed task state from generic code.
  7435. */
  7436. if (!(task->flags & PF_EXITING))
  7437. return;
  7438. task_function_call(task, __perf_cgroup_move, task);
  7439. }
  7440. struct cgroup_subsys perf_event_cgrp_subsys = {
  7441. .css_alloc = perf_cgroup_css_alloc,
  7442. .css_free = perf_cgroup_css_free,
  7443. .exit = perf_cgroup_exit,
  7444. .attach = perf_cgroup_attach,
  7445. };
  7446. #endif /* CONFIG_CGROUP_PERF */