core.c 328 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Performance events core code:
  4. *
  5. * Copyright (C) 2008 Thomas Gleixner <[email protected]>
  6. * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
  7. * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
  8. * Copyright © 2009 Paul Mackerras, IBM Corp. <[email protected]>
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/mm.h>
  12. #include <linux/cpu.h>
  13. #include <linux/smp.h>
  14. #include <linux/idr.h>
  15. #include <linux/file.h>
  16. #include <linux/poll.h>
  17. #include <linux/slab.h>
  18. #include <linux/hash.h>
  19. #include <linux/tick.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/dcache.h>
  22. #include <linux/percpu.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/reboot.h>
  25. #include <linux/vmstat.h>
  26. #include <linux/device.h>
  27. #include <linux/export.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/hardirq.h>
  30. #include <linux/hugetlb.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 <linux/namei.h>
  47. #include <linux/parser.h>
  48. #include <linux/sched/clock.h>
  49. #include <linux/sched/mm.h>
  50. #include <linux/proc_ns.h>
  51. #include <linux/mount.h>
  52. #include <linux/min_heap.h>
  53. #include <linux/highmem.h>
  54. #include <linux/pgtable.h>
  55. #include <linux/buildid.h>
  56. #include <linux/task_work.h>
  57. #include "internal.h"
  58. #include <asm/irq_regs.h>
  59. typedef int (*remote_function_f)(void *);
  60. struct remote_function_call {
  61. struct task_struct *p;
  62. remote_function_f func;
  63. void *info;
  64. int ret;
  65. };
  66. static void remote_function(void *data)
  67. {
  68. struct remote_function_call *tfc = data;
  69. struct task_struct *p = tfc->p;
  70. if (p) {
  71. /* -EAGAIN */
  72. if (task_cpu(p) != smp_processor_id())
  73. return;
  74. /*
  75. * Now that we're on right CPU with IRQs disabled, we can test
  76. * if we hit the right task without races.
  77. */
  78. tfc->ret = -ESRCH; /* No such (running) process */
  79. if (p != current)
  80. return;
  81. }
  82. tfc->ret = tfc->func(tfc->info);
  83. }
  84. /**
  85. * task_function_call - call a function on the cpu on which a task runs
  86. * @p: the task to evaluate
  87. * @func: the function to be called
  88. * @info: the function call argument
  89. *
  90. * Calls the function @func when the task is currently running. This might
  91. * be on the current CPU, which just calls the function directly. This will
  92. * retry due to any failures in smp_call_function_single(), such as if the
  93. * task_cpu() goes offline concurrently.
  94. *
  95. * returns @func return value or -ESRCH or -ENXIO when the process isn't running
  96. */
  97. static int
  98. task_function_call(struct task_struct *p, remote_function_f func, void *info)
  99. {
  100. struct remote_function_call data = {
  101. .p = p,
  102. .func = func,
  103. .info = info,
  104. .ret = -EAGAIN,
  105. };
  106. int ret;
  107. for (;;) {
  108. ret = smp_call_function_single(task_cpu(p), remote_function,
  109. &data, 1);
  110. if (!ret)
  111. ret = data.ret;
  112. if (ret != -EAGAIN)
  113. break;
  114. cond_resched();
  115. }
  116. return ret;
  117. }
  118. /**
  119. * cpu_function_call - call a function on the cpu
  120. * @cpu: target cpu to queue this function
  121. * @func: the function to be called
  122. * @info: the function call argument
  123. *
  124. * Calls the function @func on the remote cpu.
  125. *
  126. * returns: @func return value or -ENXIO when the cpu is offline
  127. */
  128. static int cpu_function_call(int cpu, remote_function_f func, void *info)
  129. {
  130. struct remote_function_call data = {
  131. .p = NULL,
  132. .func = func,
  133. .info = info,
  134. .ret = -ENXIO, /* No such CPU */
  135. };
  136. smp_call_function_single(cpu, remote_function, &data, 1);
  137. return data.ret;
  138. }
  139. static inline struct perf_cpu_context *
  140. __get_cpu_context(struct perf_event_context *ctx)
  141. {
  142. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  143. }
  144. static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
  145. struct perf_event_context *ctx)
  146. {
  147. raw_spin_lock(&cpuctx->ctx.lock);
  148. if (ctx)
  149. raw_spin_lock(&ctx->lock);
  150. }
  151. static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
  152. struct perf_event_context *ctx)
  153. {
  154. if (ctx)
  155. raw_spin_unlock(&ctx->lock);
  156. raw_spin_unlock(&cpuctx->ctx.lock);
  157. }
  158. #define TASK_TOMBSTONE ((void *)-1L)
  159. static bool is_kernel_event(struct perf_event *event)
  160. {
  161. return READ_ONCE(event->owner) == TASK_TOMBSTONE;
  162. }
  163. /*
  164. * On task ctx scheduling...
  165. *
  166. * When !ctx->nr_events a task context will not be scheduled. This means
  167. * we can disable the scheduler hooks (for performance) without leaving
  168. * pending task ctx state.
  169. *
  170. * This however results in two special cases:
  171. *
  172. * - removing the last event from a task ctx; this is relatively straight
  173. * forward and is done in __perf_remove_from_context.
  174. *
  175. * - adding the first event to a task ctx; this is tricky because we cannot
  176. * rely on ctx->is_active and therefore cannot use event_function_call().
  177. * See perf_install_in_context().
  178. *
  179. * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
  180. */
  181. typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
  182. struct perf_event_context *, void *);
  183. struct event_function_struct {
  184. struct perf_event *event;
  185. event_f func;
  186. void *data;
  187. };
  188. static int event_function(void *info)
  189. {
  190. struct event_function_struct *efs = info;
  191. struct perf_event *event = efs->event;
  192. struct perf_event_context *ctx = event->ctx;
  193. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  194. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  195. int ret = 0;
  196. lockdep_assert_irqs_disabled();
  197. perf_ctx_lock(cpuctx, task_ctx);
  198. /*
  199. * Since we do the IPI call without holding ctx->lock things can have
  200. * changed, double check we hit the task we set out to hit.
  201. */
  202. if (ctx->task) {
  203. if (ctx->task != current) {
  204. ret = -ESRCH;
  205. goto unlock;
  206. }
  207. /*
  208. * We only use event_function_call() on established contexts,
  209. * and event_function() is only ever called when active (or
  210. * rather, we'll have bailed in task_function_call() or the
  211. * above ctx->task != current test), therefore we must have
  212. * ctx->is_active here.
  213. */
  214. WARN_ON_ONCE(!ctx->is_active);
  215. /*
  216. * And since we have ctx->is_active, cpuctx->task_ctx must
  217. * match.
  218. */
  219. WARN_ON_ONCE(task_ctx != ctx);
  220. } else {
  221. WARN_ON_ONCE(&cpuctx->ctx != ctx);
  222. }
  223. efs->func(event, cpuctx, ctx, efs->data);
  224. unlock:
  225. perf_ctx_unlock(cpuctx, task_ctx);
  226. return ret;
  227. }
  228. static void event_function_call(struct perf_event *event, event_f func, void *data)
  229. {
  230. struct perf_event_context *ctx = event->ctx;
  231. struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
  232. struct event_function_struct efs = {
  233. .event = event,
  234. .func = func,
  235. .data = data,
  236. };
  237. if (!event->parent) {
  238. /*
  239. * If this is a !child event, we must hold ctx::mutex to
  240. * stabilize the event->ctx relation. See
  241. * perf_event_ctx_lock().
  242. */
  243. lockdep_assert_held(&ctx->mutex);
  244. }
  245. if (!task) {
  246. cpu_function_call(event->cpu, event_function, &efs);
  247. return;
  248. }
  249. if (task == TASK_TOMBSTONE)
  250. return;
  251. again:
  252. if (!task_function_call(task, event_function, &efs))
  253. return;
  254. raw_spin_lock_irq(&ctx->lock);
  255. /*
  256. * Reload the task pointer, it might have been changed by
  257. * a concurrent perf_event_context_sched_out().
  258. */
  259. task = ctx->task;
  260. if (task == TASK_TOMBSTONE) {
  261. raw_spin_unlock_irq(&ctx->lock);
  262. return;
  263. }
  264. if (ctx->is_active) {
  265. raw_spin_unlock_irq(&ctx->lock);
  266. goto again;
  267. }
  268. func(event, NULL, ctx, data);
  269. raw_spin_unlock_irq(&ctx->lock);
  270. }
  271. /*
  272. * Similar to event_function_call() + event_function(), but hard assumes IRQs
  273. * are already disabled and we're on the right CPU.
  274. */
  275. static void event_function_local(struct perf_event *event, event_f func, void *data)
  276. {
  277. struct perf_event_context *ctx = event->ctx;
  278. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  279. struct task_struct *task = READ_ONCE(ctx->task);
  280. struct perf_event_context *task_ctx = NULL;
  281. lockdep_assert_irqs_disabled();
  282. if (task) {
  283. if (task == TASK_TOMBSTONE)
  284. return;
  285. task_ctx = ctx;
  286. }
  287. perf_ctx_lock(cpuctx, task_ctx);
  288. task = ctx->task;
  289. if (task == TASK_TOMBSTONE)
  290. goto unlock;
  291. if (task) {
  292. /*
  293. * We must be either inactive or active and the right task,
  294. * otherwise we're screwed, since we cannot IPI to somewhere
  295. * else.
  296. */
  297. if (ctx->is_active) {
  298. if (WARN_ON_ONCE(task != current))
  299. goto unlock;
  300. if (WARN_ON_ONCE(cpuctx->task_ctx != ctx))
  301. goto unlock;
  302. }
  303. } else {
  304. WARN_ON_ONCE(&cpuctx->ctx != ctx);
  305. }
  306. func(event, cpuctx, ctx, data);
  307. unlock:
  308. perf_ctx_unlock(cpuctx, task_ctx);
  309. }
  310. #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
  311. PERF_FLAG_FD_OUTPUT |\
  312. PERF_FLAG_PID_CGROUP |\
  313. PERF_FLAG_FD_CLOEXEC)
  314. /*
  315. * branch priv levels that need permission checks
  316. */
  317. #define PERF_SAMPLE_BRANCH_PERM_PLM \
  318. (PERF_SAMPLE_BRANCH_KERNEL |\
  319. PERF_SAMPLE_BRANCH_HV)
  320. enum event_type_t {
  321. EVENT_FLEXIBLE = 0x1,
  322. EVENT_PINNED = 0x2,
  323. EVENT_TIME = 0x4,
  324. /* see ctx_resched() for details */
  325. EVENT_CPU = 0x8,
  326. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  327. };
  328. /*
  329. * perf_sched_events : >0 events exist
  330. * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
  331. */
  332. static void perf_sched_delayed(struct work_struct *work);
  333. DEFINE_STATIC_KEY_FALSE(perf_sched_events);
  334. static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
  335. static DEFINE_MUTEX(perf_sched_mutex);
  336. static atomic_t perf_sched_count;
  337. static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
  338. static DEFINE_PER_CPU(int, perf_sched_cb_usages);
  339. static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events);
  340. static atomic_t nr_mmap_events __read_mostly;
  341. static atomic_t nr_comm_events __read_mostly;
  342. static atomic_t nr_namespaces_events __read_mostly;
  343. static atomic_t nr_task_events __read_mostly;
  344. static atomic_t nr_freq_events __read_mostly;
  345. static atomic_t nr_switch_events __read_mostly;
  346. static atomic_t nr_ksymbol_events __read_mostly;
  347. static atomic_t nr_bpf_events __read_mostly;
  348. static atomic_t nr_cgroup_events __read_mostly;
  349. static atomic_t nr_text_poke_events __read_mostly;
  350. static atomic_t nr_build_id_events __read_mostly;
  351. static LIST_HEAD(pmus);
  352. static DEFINE_MUTEX(pmus_lock);
  353. static struct srcu_struct pmus_srcu;
  354. static cpumask_var_t perf_online_mask;
  355. static struct kmem_cache *perf_event_cache;
  356. /*
  357. * perf event paranoia level:
  358. * -1 - not paranoid at all
  359. * 0 - disallow raw tracepoint access for unpriv
  360. * 1 - disallow cpu events for unpriv
  361. * 2 - disallow kernel profiling for unpriv
  362. */
  363. int sysctl_perf_event_paranoid __read_mostly = 2;
  364. /* Minimum for 512 kiB + 1 user control page */
  365. int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
  366. /*
  367. * max perf event sample rate
  368. */
  369. #define DEFAULT_MAX_SAMPLE_RATE 100000
  370. #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
  371. #define DEFAULT_CPU_TIME_MAX_PERCENT 25
  372. int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
  373. static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
  374. static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
  375. static int perf_sample_allowed_ns __read_mostly =
  376. DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
  377. static void update_perf_cpu_limits(void)
  378. {
  379. u64 tmp = perf_sample_period_ns;
  380. tmp *= sysctl_perf_cpu_time_max_percent;
  381. tmp = div_u64(tmp, 100);
  382. if (!tmp)
  383. tmp = 1;
  384. WRITE_ONCE(perf_sample_allowed_ns, tmp);
  385. }
  386. static bool perf_rotate_context(struct perf_cpu_context *cpuctx);
  387. int perf_proc_update_handler(struct ctl_table *table, int write,
  388. void *buffer, size_t *lenp, loff_t *ppos)
  389. {
  390. int ret;
  391. int perf_cpu = sysctl_perf_cpu_time_max_percent;
  392. /*
  393. * If throttling is disabled don't allow the write:
  394. */
  395. if (write && (perf_cpu == 100 || perf_cpu == 0))
  396. return -EINVAL;
  397. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  398. if (ret || !write)
  399. return ret;
  400. max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
  401. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  402. update_perf_cpu_limits();
  403. return 0;
  404. }
  405. int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
  406. int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  407. void *buffer, size_t *lenp, loff_t *ppos)
  408. {
  409. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  410. if (ret || !write)
  411. return ret;
  412. if (sysctl_perf_cpu_time_max_percent == 100 ||
  413. sysctl_perf_cpu_time_max_percent == 0) {
  414. printk(KERN_WARNING
  415. "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
  416. WRITE_ONCE(perf_sample_allowed_ns, 0);
  417. } else {
  418. update_perf_cpu_limits();
  419. }
  420. return 0;
  421. }
  422. /*
  423. * perf samples are done in some very critical code paths (NMIs).
  424. * If they take too much CPU time, the system can lock up and not
  425. * get any real work done. This will drop the sample rate when
  426. * we detect that events are taking too long.
  427. */
  428. #define NR_ACCUMULATED_SAMPLES 128
  429. static DEFINE_PER_CPU(u64, running_sample_length);
  430. static u64 __report_avg;
  431. static u64 __report_allowed;
  432. static void perf_duration_warn(struct irq_work *w)
  433. {
  434. printk_ratelimited(KERN_INFO
  435. "perf: interrupt took too long (%lld > %lld), lowering "
  436. "kernel.perf_event_max_sample_rate to %d\n",
  437. __report_avg, __report_allowed,
  438. sysctl_perf_event_sample_rate);
  439. }
  440. static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
  441. void perf_sample_event_took(u64 sample_len_ns)
  442. {
  443. u64 max_len = READ_ONCE(perf_sample_allowed_ns);
  444. u64 running_len;
  445. u64 avg_len;
  446. u32 max;
  447. if (max_len == 0)
  448. return;
  449. /* Decay the counter by 1 average sample. */
  450. running_len = __this_cpu_read(running_sample_length);
  451. running_len -= running_len/NR_ACCUMULATED_SAMPLES;
  452. running_len += sample_len_ns;
  453. __this_cpu_write(running_sample_length, running_len);
  454. /*
  455. * Note: this will be biased artifically low until we have
  456. * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
  457. * from having to maintain a count.
  458. */
  459. avg_len = running_len/NR_ACCUMULATED_SAMPLES;
  460. if (avg_len <= max_len)
  461. return;
  462. __report_avg = avg_len;
  463. __report_allowed = max_len;
  464. /*
  465. * Compute a throttle threshold 25% below the current duration.
  466. */
  467. avg_len += avg_len / 4;
  468. max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
  469. if (avg_len < max)
  470. max /= (u32)avg_len;
  471. else
  472. max = 1;
  473. WRITE_ONCE(perf_sample_allowed_ns, avg_len);
  474. WRITE_ONCE(max_samples_per_tick, max);
  475. sysctl_perf_event_sample_rate = max * HZ;
  476. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  477. if (!irq_work_queue(&perf_duration_work)) {
  478. early_printk("perf: interrupt took too long (%lld > %lld), lowering "
  479. "kernel.perf_event_max_sample_rate to %d\n",
  480. __report_avg, __report_allowed,
  481. sysctl_perf_event_sample_rate);
  482. }
  483. }
  484. static atomic64_t perf_event_id;
  485. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  486. enum event_type_t event_type);
  487. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  488. enum event_type_t event_type);
  489. static void update_context_time(struct perf_event_context *ctx);
  490. static u64 perf_event_time(struct perf_event *event);
  491. void __weak perf_event_print_debug(void) { }
  492. static inline u64 perf_clock(void)
  493. {
  494. return local_clock();
  495. }
  496. static inline u64 perf_event_clock(struct perf_event *event)
  497. {
  498. return event->clock();
  499. }
  500. /*
  501. * State based event timekeeping...
  502. *
  503. * The basic idea is to use event->state to determine which (if any) time
  504. * fields to increment with the current delta. This means we only need to
  505. * update timestamps when we change state or when they are explicitly requested
  506. * (read).
  507. *
  508. * Event groups make things a little more complicated, but not terribly so. The
  509. * rules for a group are that if the group leader is OFF the entire group is
  510. * OFF, irrespecive of what the group member states are. This results in
  511. * __perf_effective_state().
  512. *
  513. * A futher ramification is that when a group leader flips between OFF and
  514. * !OFF, we need to update all group member times.
  515. *
  516. *
  517. * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we
  518. * need to make sure the relevant context time is updated before we try and
  519. * update our timestamps.
  520. */
  521. static __always_inline enum perf_event_state
  522. __perf_effective_state(struct perf_event *event)
  523. {
  524. struct perf_event *leader = event->group_leader;
  525. if (leader->state <= PERF_EVENT_STATE_OFF)
  526. return leader->state;
  527. return event->state;
  528. }
  529. static __always_inline void
  530. __perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running)
  531. {
  532. enum perf_event_state state = __perf_effective_state(event);
  533. u64 delta = now - event->tstamp;
  534. *enabled = event->total_time_enabled;
  535. if (state >= PERF_EVENT_STATE_INACTIVE)
  536. *enabled += delta;
  537. *running = event->total_time_running;
  538. if (state >= PERF_EVENT_STATE_ACTIVE)
  539. *running += delta;
  540. }
  541. static void perf_event_update_time(struct perf_event *event)
  542. {
  543. u64 now = perf_event_time(event);
  544. __perf_update_times(event, now, &event->total_time_enabled,
  545. &event->total_time_running);
  546. event->tstamp = now;
  547. }
  548. static void perf_event_update_sibling_time(struct perf_event *leader)
  549. {
  550. struct perf_event *sibling;
  551. for_each_sibling_event(sibling, leader)
  552. perf_event_update_time(sibling);
  553. }
  554. static void
  555. perf_event_set_state(struct perf_event *event, enum perf_event_state state)
  556. {
  557. if (event->state == state)
  558. return;
  559. perf_event_update_time(event);
  560. /*
  561. * If a group leader gets enabled/disabled all its siblings
  562. * are affected too.
  563. */
  564. if ((event->state < 0) ^ (state < 0))
  565. perf_event_update_sibling_time(event);
  566. WRITE_ONCE(event->state, state);
  567. }
  568. /*
  569. * UP store-release, load-acquire
  570. */
  571. #define __store_release(ptr, val) \
  572. do { \
  573. barrier(); \
  574. WRITE_ONCE(*(ptr), (val)); \
  575. } while (0)
  576. #define __load_acquire(ptr) \
  577. ({ \
  578. __unqual_scalar_typeof(*(ptr)) ___p = READ_ONCE(*(ptr)); \
  579. barrier(); \
  580. ___p; \
  581. })
  582. #ifdef CONFIG_CGROUP_PERF
  583. static inline bool
  584. perf_cgroup_match(struct perf_event *event)
  585. {
  586. struct perf_event_context *ctx = event->ctx;
  587. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  588. /* @event doesn't care about cgroup */
  589. if (!event->cgrp)
  590. return true;
  591. /* wants specific cgroup scope but @cpuctx isn't associated with any */
  592. if (!cpuctx->cgrp)
  593. return false;
  594. /*
  595. * Cgroup scoping is recursive. An event enabled for a cgroup is
  596. * also enabled for all its descendant cgroups. If @cpuctx's
  597. * cgroup is a descendant of @event's (the test covers identity
  598. * case), it's a match.
  599. */
  600. return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
  601. event->cgrp->css.cgroup);
  602. }
  603. static inline void perf_detach_cgroup(struct perf_event *event)
  604. {
  605. css_put(&event->cgrp->css);
  606. event->cgrp = NULL;
  607. }
  608. static inline int is_cgroup_event(struct perf_event *event)
  609. {
  610. return event->cgrp != NULL;
  611. }
  612. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  613. {
  614. struct perf_cgroup_info *t;
  615. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  616. return t->time;
  617. }
  618. static inline u64 perf_cgroup_event_time_now(struct perf_event *event, u64 now)
  619. {
  620. struct perf_cgroup_info *t;
  621. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  622. if (!__load_acquire(&t->active))
  623. return t->time;
  624. now += READ_ONCE(t->timeoffset);
  625. return now;
  626. }
  627. static inline void __update_cgrp_time(struct perf_cgroup_info *info, u64 now, bool adv)
  628. {
  629. if (adv)
  630. info->time += now - info->timestamp;
  631. info->timestamp = now;
  632. /*
  633. * see update_context_time()
  634. */
  635. WRITE_ONCE(info->timeoffset, info->time - info->timestamp);
  636. }
  637. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx, bool final)
  638. {
  639. struct perf_cgroup *cgrp = cpuctx->cgrp;
  640. struct cgroup_subsys_state *css;
  641. struct perf_cgroup_info *info;
  642. if (cgrp) {
  643. u64 now = perf_clock();
  644. for (css = &cgrp->css; css; css = css->parent) {
  645. cgrp = container_of(css, struct perf_cgroup, css);
  646. info = this_cpu_ptr(cgrp->info);
  647. __update_cgrp_time(info, now, true);
  648. if (final)
  649. __store_release(&info->active, 0);
  650. }
  651. }
  652. }
  653. static inline void update_cgrp_time_from_event(struct perf_event *event)
  654. {
  655. struct perf_cgroup_info *info;
  656. /*
  657. * ensure we access cgroup data only when needed and
  658. * when we know the cgroup is pinned (css_get)
  659. */
  660. if (!is_cgroup_event(event))
  661. return;
  662. info = this_cpu_ptr(event->cgrp->info);
  663. /*
  664. * Do not update time when cgroup is not active
  665. */
  666. if (info->active)
  667. __update_cgrp_time(info, perf_clock(), true);
  668. }
  669. static inline void
  670. perf_cgroup_set_timestamp(struct perf_cpu_context *cpuctx)
  671. {
  672. struct perf_event_context *ctx = &cpuctx->ctx;
  673. struct perf_cgroup *cgrp = cpuctx->cgrp;
  674. struct perf_cgroup_info *info;
  675. struct cgroup_subsys_state *css;
  676. /*
  677. * ctx->lock held by caller
  678. * ensure we do not access cgroup data
  679. * unless we have the cgroup pinned (css_get)
  680. */
  681. if (!cgrp)
  682. return;
  683. WARN_ON_ONCE(!ctx->nr_cgroups);
  684. for (css = &cgrp->css; css; css = css->parent) {
  685. cgrp = container_of(css, struct perf_cgroup, css);
  686. info = this_cpu_ptr(cgrp->info);
  687. __update_cgrp_time(info, ctx->timestamp, false);
  688. __store_release(&info->active, 1);
  689. }
  690. }
  691. static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list);
  692. /*
  693. * reschedule events based on the cgroup constraint of task.
  694. */
  695. static void perf_cgroup_switch(struct task_struct *task)
  696. {
  697. struct perf_cgroup *cgrp;
  698. struct perf_cpu_context *cpuctx, *tmp;
  699. struct list_head *list;
  700. unsigned long flags;
  701. /*
  702. * Disable interrupts and preemption to avoid this CPU's
  703. * cgrp_cpuctx_entry to change under us.
  704. */
  705. local_irq_save(flags);
  706. cgrp = perf_cgroup_from_task(task, NULL);
  707. list = this_cpu_ptr(&cgrp_cpuctx_list);
  708. list_for_each_entry_safe(cpuctx, tmp, list, cgrp_cpuctx_entry) {
  709. WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0);
  710. if (READ_ONCE(cpuctx->cgrp) == cgrp)
  711. continue;
  712. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  713. perf_pmu_disable(cpuctx->ctx.pmu);
  714. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  715. /*
  716. * must not be done before ctxswout due
  717. * to update_cgrp_time_from_cpuctx() in
  718. * ctx_sched_out()
  719. */
  720. cpuctx->cgrp = cgrp;
  721. /*
  722. * set cgrp before ctxsw in to allow
  723. * perf_cgroup_set_timestamp() in ctx_sched_in()
  724. * to not have to pass task around
  725. */
  726. cpu_ctx_sched_in(cpuctx, EVENT_ALL);
  727. perf_pmu_enable(cpuctx->ctx.pmu);
  728. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  729. }
  730. local_irq_restore(flags);
  731. }
  732. static int perf_cgroup_ensure_storage(struct perf_event *event,
  733. struct cgroup_subsys_state *css)
  734. {
  735. struct perf_cpu_context *cpuctx;
  736. struct perf_event **storage;
  737. int cpu, heap_size, ret = 0;
  738. /*
  739. * Allow storage to have sufficent space for an iterator for each
  740. * possibly nested cgroup plus an iterator for events with no cgroup.
  741. */
  742. for (heap_size = 1; css; css = css->parent)
  743. heap_size++;
  744. for_each_possible_cpu(cpu) {
  745. cpuctx = per_cpu_ptr(event->pmu->pmu_cpu_context, cpu);
  746. if (heap_size <= cpuctx->heap_size)
  747. continue;
  748. storage = kmalloc_node(heap_size * sizeof(struct perf_event *),
  749. GFP_KERNEL, cpu_to_node(cpu));
  750. if (!storage) {
  751. ret = -ENOMEM;
  752. break;
  753. }
  754. raw_spin_lock_irq(&cpuctx->ctx.lock);
  755. if (cpuctx->heap_size < heap_size) {
  756. swap(cpuctx->heap, storage);
  757. if (storage == cpuctx->heap_default)
  758. storage = NULL;
  759. cpuctx->heap_size = heap_size;
  760. }
  761. raw_spin_unlock_irq(&cpuctx->ctx.lock);
  762. kfree(storage);
  763. }
  764. return ret;
  765. }
  766. static inline int perf_cgroup_connect(int fd, struct perf_event *event,
  767. struct perf_event_attr *attr,
  768. struct perf_event *group_leader)
  769. {
  770. struct perf_cgroup *cgrp;
  771. struct cgroup_subsys_state *css;
  772. struct fd f = fdget(fd);
  773. int ret = 0;
  774. if (!f.file)
  775. return -EBADF;
  776. css = css_tryget_online_from_dir(f.file->f_path.dentry,
  777. &perf_event_cgrp_subsys);
  778. if (IS_ERR(css)) {
  779. ret = PTR_ERR(css);
  780. goto out;
  781. }
  782. ret = perf_cgroup_ensure_storage(event, css);
  783. if (ret)
  784. goto out;
  785. cgrp = container_of(css, struct perf_cgroup, css);
  786. event->cgrp = cgrp;
  787. /*
  788. * all events in a group must monitor
  789. * the same cgroup because a task belongs
  790. * to only one perf cgroup at a time
  791. */
  792. if (group_leader && group_leader->cgrp != cgrp) {
  793. perf_detach_cgroup(event);
  794. ret = -EINVAL;
  795. }
  796. out:
  797. fdput(f);
  798. return ret;
  799. }
  800. static inline void
  801. perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx)
  802. {
  803. struct perf_cpu_context *cpuctx;
  804. if (!is_cgroup_event(event))
  805. return;
  806. /*
  807. * Because cgroup events are always per-cpu events,
  808. * @ctx == &cpuctx->ctx.
  809. */
  810. cpuctx = container_of(ctx, struct perf_cpu_context, ctx);
  811. if (ctx->nr_cgroups++)
  812. return;
  813. cpuctx->cgrp = perf_cgroup_from_task(current, ctx);
  814. list_add(&cpuctx->cgrp_cpuctx_entry,
  815. per_cpu_ptr(&cgrp_cpuctx_list, event->cpu));
  816. }
  817. static inline void
  818. perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx)
  819. {
  820. struct perf_cpu_context *cpuctx;
  821. if (!is_cgroup_event(event))
  822. return;
  823. /*
  824. * Because cgroup events are always per-cpu events,
  825. * @ctx == &cpuctx->ctx.
  826. */
  827. cpuctx = container_of(ctx, struct perf_cpu_context, ctx);
  828. if (--ctx->nr_cgroups)
  829. return;
  830. cpuctx->cgrp = NULL;
  831. list_del(&cpuctx->cgrp_cpuctx_entry);
  832. }
  833. #else /* !CONFIG_CGROUP_PERF */
  834. static inline bool
  835. perf_cgroup_match(struct perf_event *event)
  836. {
  837. return true;
  838. }
  839. static inline void perf_detach_cgroup(struct perf_event *event)
  840. {}
  841. static inline int is_cgroup_event(struct perf_event *event)
  842. {
  843. return 0;
  844. }
  845. static inline void update_cgrp_time_from_event(struct perf_event *event)
  846. {
  847. }
  848. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx,
  849. bool final)
  850. {
  851. }
  852. static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
  853. struct perf_event_attr *attr,
  854. struct perf_event *group_leader)
  855. {
  856. return -EINVAL;
  857. }
  858. static inline void
  859. perf_cgroup_set_timestamp(struct perf_cpu_context *cpuctx)
  860. {
  861. }
  862. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  863. {
  864. return 0;
  865. }
  866. static inline u64 perf_cgroup_event_time_now(struct perf_event *event, u64 now)
  867. {
  868. return 0;
  869. }
  870. static inline void
  871. perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx)
  872. {
  873. }
  874. static inline void
  875. perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx)
  876. {
  877. }
  878. static void perf_cgroup_switch(struct task_struct *task)
  879. {
  880. }
  881. #endif
  882. /*
  883. * set default to be dependent on timer tick just
  884. * like original code
  885. */
  886. #define PERF_CPU_HRTIMER (1000 / HZ)
  887. /*
  888. * function must be called with interrupts disabled
  889. */
  890. static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
  891. {
  892. struct perf_cpu_context *cpuctx;
  893. bool rotations;
  894. lockdep_assert_irqs_disabled();
  895. cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
  896. rotations = perf_rotate_context(cpuctx);
  897. raw_spin_lock(&cpuctx->hrtimer_lock);
  898. if (rotations)
  899. hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
  900. else
  901. cpuctx->hrtimer_active = 0;
  902. raw_spin_unlock(&cpuctx->hrtimer_lock);
  903. return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
  904. }
  905. static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  906. {
  907. struct hrtimer *timer = &cpuctx->hrtimer;
  908. struct pmu *pmu = cpuctx->ctx.pmu;
  909. u64 interval;
  910. /* no multiplexing needed for SW PMU */
  911. if (pmu->task_ctx_nr == perf_sw_context)
  912. return;
  913. /*
  914. * check default is sane, if not set then force to
  915. * default interval (1/tick)
  916. */
  917. interval = pmu->hrtimer_interval_ms;
  918. if (interval < 1)
  919. interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
  920. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
  921. raw_spin_lock_init(&cpuctx->hrtimer_lock);
  922. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
  923. timer->function = perf_mux_hrtimer_handler;
  924. }
  925. static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
  926. {
  927. struct hrtimer *timer = &cpuctx->hrtimer;
  928. struct pmu *pmu = cpuctx->ctx.pmu;
  929. unsigned long flags;
  930. /* not for SW PMU */
  931. if (pmu->task_ctx_nr == perf_sw_context)
  932. return 0;
  933. raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
  934. if (!cpuctx->hrtimer_active) {
  935. cpuctx->hrtimer_active = 1;
  936. hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
  937. hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD);
  938. }
  939. raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
  940. return 0;
  941. }
  942. static int perf_mux_hrtimer_restart_ipi(void *arg)
  943. {
  944. return perf_mux_hrtimer_restart(arg);
  945. }
  946. void perf_pmu_disable(struct pmu *pmu)
  947. {
  948. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  949. if (!(*count)++)
  950. pmu->pmu_disable(pmu);
  951. }
  952. void perf_pmu_enable(struct pmu *pmu)
  953. {
  954. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  955. if (!--(*count))
  956. pmu->pmu_enable(pmu);
  957. }
  958. static DEFINE_PER_CPU(struct list_head, active_ctx_list);
  959. /*
  960. * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
  961. * perf_event_task_tick() are fully serialized because they're strictly cpu
  962. * affine and perf_event_ctx{activate,deactivate} are called with IRQs
  963. * disabled, while perf_event_task_tick is called from IRQ context.
  964. */
  965. static void perf_event_ctx_activate(struct perf_event_context *ctx)
  966. {
  967. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  968. lockdep_assert_irqs_disabled();
  969. WARN_ON(!list_empty(&ctx->active_ctx_list));
  970. list_add(&ctx->active_ctx_list, head);
  971. }
  972. static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
  973. {
  974. lockdep_assert_irqs_disabled();
  975. WARN_ON(list_empty(&ctx->active_ctx_list));
  976. list_del_init(&ctx->active_ctx_list);
  977. }
  978. static void get_ctx(struct perf_event_context *ctx)
  979. {
  980. refcount_inc(&ctx->refcount);
  981. }
  982. static void *alloc_task_ctx_data(struct pmu *pmu)
  983. {
  984. if (pmu->task_ctx_cache)
  985. return kmem_cache_zalloc(pmu->task_ctx_cache, GFP_KERNEL);
  986. return NULL;
  987. }
  988. static void free_task_ctx_data(struct pmu *pmu, void *task_ctx_data)
  989. {
  990. if (pmu->task_ctx_cache && task_ctx_data)
  991. kmem_cache_free(pmu->task_ctx_cache, task_ctx_data);
  992. }
  993. static void free_ctx(struct rcu_head *head)
  994. {
  995. struct perf_event_context *ctx;
  996. ctx = container_of(head, struct perf_event_context, rcu_head);
  997. free_task_ctx_data(ctx->pmu, ctx->task_ctx_data);
  998. kfree(ctx);
  999. }
  1000. static void put_ctx(struct perf_event_context *ctx)
  1001. {
  1002. if (refcount_dec_and_test(&ctx->refcount)) {
  1003. if (ctx->parent_ctx)
  1004. put_ctx(ctx->parent_ctx);
  1005. if (ctx->task && ctx->task != TASK_TOMBSTONE)
  1006. put_task_struct(ctx->task);
  1007. call_rcu(&ctx->rcu_head, free_ctx);
  1008. }
  1009. }
  1010. /*
  1011. * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
  1012. * perf_pmu_migrate_context() we need some magic.
  1013. *
  1014. * Those places that change perf_event::ctx will hold both
  1015. * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
  1016. *
  1017. * Lock ordering is by mutex address. There are two other sites where
  1018. * perf_event_context::mutex nests and those are:
  1019. *
  1020. * - perf_event_exit_task_context() [ child , 0 ]
  1021. * perf_event_exit_event()
  1022. * put_event() [ parent, 1 ]
  1023. *
  1024. * - perf_event_init_context() [ parent, 0 ]
  1025. * inherit_task_group()
  1026. * inherit_group()
  1027. * inherit_event()
  1028. * perf_event_alloc()
  1029. * perf_init_event()
  1030. * perf_try_init_event() [ child , 1 ]
  1031. *
  1032. * While it appears there is an obvious deadlock here -- the parent and child
  1033. * nesting levels are inverted between the two. This is in fact safe because
  1034. * life-time rules separate them. That is an exiting task cannot fork, and a
  1035. * spawning task cannot (yet) exit.
  1036. *
  1037. * But remember that these are parent<->child context relations, and
  1038. * migration does not affect children, therefore these two orderings should not
  1039. * interact.
  1040. *
  1041. * The change in perf_event::ctx does not affect children (as claimed above)
  1042. * because the sys_perf_event_open() case will install a new event and break
  1043. * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
  1044. * concerned with cpuctx and that doesn't have children.
  1045. *
  1046. * The places that change perf_event::ctx will issue:
  1047. *
  1048. * perf_remove_from_context();
  1049. * synchronize_rcu();
  1050. * perf_install_in_context();
  1051. *
  1052. * to affect the change. The remove_from_context() + synchronize_rcu() should
  1053. * quiesce the event, after which we can install it in the new location. This
  1054. * means that only external vectors (perf_fops, prctl) can perturb the event
  1055. * while in transit. Therefore all such accessors should also acquire
  1056. * perf_event_context::mutex to serialize against this.
  1057. *
  1058. * However; because event->ctx can change while we're waiting to acquire
  1059. * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
  1060. * function.
  1061. *
  1062. * Lock order:
  1063. * exec_update_lock
  1064. * task_struct::perf_event_mutex
  1065. * perf_event_context::mutex
  1066. * perf_event::child_mutex;
  1067. * perf_event_context::lock
  1068. * perf_event::mmap_mutex
  1069. * mmap_lock
  1070. * perf_addr_filters_head::lock
  1071. *
  1072. * cpu_hotplug_lock
  1073. * pmus_lock
  1074. * cpuctx->mutex / perf_event_context::mutex
  1075. */
  1076. static struct perf_event_context *
  1077. perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
  1078. {
  1079. struct perf_event_context *ctx;
  1080. again:
  1081. rcu_read_lock();
  1082. ctx = READ_ONCE(event->ctx);
  1083. if (!refcount_inc_not_zero(&ctx->refcount)) {
  1084. rcu_read_unlock();
  1085. goto again;
  1086. }
  1087. rcu_read_unlock();
  1088. mutex_lock_nested(&ctx->mutex, nesting);
  1089. if (event->ctx != ctx) {
  1090. mutex_unlock(&ctx->mutex);
  1091. put_ctx(ctx);
  1092. goto again;
  1093. }
  1094. return ctx;
  1095. }
  1096. static inline struct perf_event_context *
  1097. perf_event_ctx_lock(struct perf_event *event)
  1098. {
  1099. return perf_event_ctx_lock_nested(event, 0);
  1100. }
  1101. static void perf_event_ctx_unlock(struct perf_event *event,
  1102. struct perf_event_context *ctx)
  1103. {
  1104. mutex_unlock(&ctx->mutex);
  1105. put_ctx(ctx);
  1106. }
  1107. /*
  1108. * This must be done under the ctx->lock, such as to serialize against
  1109. * context_equiv(), therefore we cannot call put_ctx() since that might end up
  1110. * calling scheduler related locks and ctx->lock nests inside those.
  1111. */
  1112. static __must_check struct perf_event_context *
  1113. unclone_ctx(struct perf_event_context *ctx)
  1114. {
  1115. struct perf_event_context *parent_ctx = ctx->parent_ctx;
  1116. lockdep_assert_held(&ctx->lock);
  1117. if (parent_ctx)
  1118. ctx->parent_ctx = NULL;
  1119. ctx->generation++;
  1120. return parent_ctx;
  1121. }
  1122. static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
  1123. enum pid_type type)
  1124. {
  1125. u32 nr;
  1126. /*
  1127. * only top level events have the pid namespace they were created in
  1128. */
  1129. if (event->parent)
  1130. event = event->parent;
  1131. nr = __task_pid_nr_ns(p, type, event->ns);
  1132. /* avoid -1 if it is idle thread or runs in another ns */
  1133. if (!nr && !pid_alive(p))
  1134. nr = -1;
  1135. return nr;
  1136. }
  1137. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  1138. {
  1139. return perf_event_pid_type(event, p, PIDTYPE_TGID);
  1140. }
  1141. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  1142. {
  1143. return perf_event_pid_type(event, p, PIDTYPE_PID);
  1144. }
  1145. /*
  1146. * If we inherit events we want to return the parent event id
  1147. * to userspace.
  1148. */
  1149. static u64 primary_event_id(struct perf_event *event)
  1150. {
  1151. u64 id = event->id;
  1152. if (event->parent)
  1153. id = event->parent->id;
  1154. return id;
  1155. }
  1156. /*
  1157. * Get the perf_event_context for a task and lock it.
  1158. *
  1159. * This has to cope with the fact that until it is locked,
  1160. * the context could get moved to another task.
  1161. */
  1162. static struct perf_event_context *
  1163. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  1164. {
  1165. struct perf_event_context *ctx;
  1166. retry:
  1167. /*
  1168. * One of the few rules of preemptible RCU is that one cannot do
  1169. * rcu_read_unlock() while holding a scheduler (or nested) lock when
  1170. * part of the read side critical section was irqs-enabled -- see
  1171. * rcu_read_unlock_special().
  1172. *
  1173. * Since ctx->lock nests under rq->lock we must ensure the entire read
  1174. * side critical section has interrupts disabled.
  1175. */
  1176. local_irq_save(*flags);
  1177. rcu_read_lock();
  1178. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  1179. if (ctx) {
  1180. /*
  1181. * If this context is a clone of another, it might
  1182. * get swapped for another underneath us by
  1183. * perf_event_task_sched_out, though the
  1184. * rcu_read_lock() protects us from any context
  1185. * getting freed. Lock the context and check if it
  1186. * got swapped before we could get the lock, and retry
  1187. * if so. If we locked the right context, then it
  1188. * can't get swapped on us any more.
  1189. */
  1190. raw_spin_lock(&ctx->lock);
  1191. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  1192. raw_spin_unlock(&ctx->lock);
  1193. rcu_read_unlock();
  1194. local_irq_restore(*flags);
  1195. goto retry;
  1196. }
  1197. if (ctx->task == TASK_TOMBSTONE ||
  1198. !refcount_inc_not_zero(&ctx->refcount)) {
  1199. raw_spin_unlock(&ctx->lock);
  1200. ctx = NULL;
  1201. } else {
  1202. WARN_ON_ONCE(ctx->task != task);
  1203. }
  1204. }
  1205. rcu_read_unlock();
  1206. if (!ctx)
  1207. local_irq_restore(*flags);
  1208. return ctx;
  1209. }
  1210. /*
  1211. * Get the context for a task and increment its pin_count so it
  1212. * can't get swapped to another task. This also increments its
  1213. * reference count so that the context can't get freed.
  1214. */
  1215. static struct perf_event_context *
  1216. perf_pin_task_context(struct task_struct *task, int ctxn)
  1217. {
  1218. struct perf_event_context *ctx;
  1219. unsigned long flags;
  1220. ctx = perf_lock_task_context(task, ctxn, &flags);
  1221. if (ctx) {
  1222. ++ctx->pin_count;
  1223. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1224. }
  1225. return ctx;
  1226. }
  1227. static void perf_unpin_context(struct perf_event_context *ctx)
  1228. {
  1229. unsigned long flags;
  1230. raw_spin_lock_irqsave(&ctx->lock, flags);
  1231. --ctx->pin_count;
  1232. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1233. }
  1234. /*
  1235. * Update the record of the current time in a context.
  1236. */
  1237. static void __update_context_time(struct perf_event_context *ctx, bool adv)
  1238. {
  1239. u64 now = perf_clock();
  1240. lockdep_assert_held(&ctx->lock);
  1241. if (adv)
  1242. ctx->time += now - ctx->timestamp;
  1243. ctx->timestamp = now;
  1244. /*
  1245. * The above: time' = time + (now - timestamp), can be re-arranged
  1246. * into: time` = now + (time - timestamp), which gives a single value
  1247. * offset to compute future time without locks on.
  1248. *
  1249. * See perf_event_time_now(), which can be used from NMI context where
  1250. * it's (obviously) not possible to acquire ctx->lock in order to read
  1251. * both the above values in a consistent manner.
  1252. */
  1253. WRITE_ONCE(ctx->timeoffset, ctx->time - ctx->timestamp);
  1254. }
  1255. static void update_context_time(struct perf_event_context *ctx)
  1256. {
  1257. __update_context_time(ctx, true);
  1258. }
  1259. static u64 perf_event_time(struct perf_event *event)
  1260. {
  1261. struct perf_event_context *ctx = event->ctx;
  1262. if (unlikely(!ctx))
  1263. return 0;
  1264. if (is_cgroup_event(event))
  1265. return perf_cgroup_event_time(event);
  1266. return ctx->time;
  1267. }
  1268. static u64 perf_event_time_now(struct perf_event *event, u64 now)
  1269. {
  1270. struct perf_event_context *ctx = event->ctx;
  1271. if (unlikely(!ctx))
  1272. return 0;
  1273. if (is_cgroup_event(event))
  1274. return perf_cgroup_event_time_now(event, now);
  1275. if (!(__load_acquire(&ctx->is_active) & EVENT_TIME))
  1276. return ctx->time;
  1277. now += READ_ONCE(ctx->timeoffset);
  1278. return now;
  1279. }
  1280. static enum event_type_t get_event_type(struct perf_event *event)
  1281. {
  1282. struct perf_event_context *ctx = event->ctx;
  1283. enum event_type_t event_type;
  1284. lockdep_assert_held(&ctx->lock);
  1285. /*
  1286. * It's 'group type', really, because if our group leader is
  1287. * pinned, so are we.
  1288. */
  1289. if (event->group_leader != event)
  1290. event = event->group_leader;
  1291. event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE;
  1292. if (!ctx->task)
  1293. event_type |= EVENT_CPU;
  1294. return event_type;
  1295. }
  1296. /*
  1297. * Helper function to initialize event group nodes.
  1298. */
  1299. static void init_event_group(struct perf_event *event)
  1300. {
  1301. RB_CLEAR_NODE(&event->group_node);
  1302. event->group_index = 0;
  1303. }
  1304. /*
  1305. * Extract pinned or flexible groups from the context
  1306. * based on event attrs bits.
  1307. */
  1308. static struct perf_event_groups *
  1309. get_event_groups(struct perf_event *event, struct perf_event_context *ctx)
  1310. {
  1311. if (event->attr.pinned)
  1312. return &ctx->pinned_groups;
  1313. else
  1314. return &ctx->flexible_groups;
  1315. }
  1316. /*
  1317. * Helper function to initializes perf_event_group trees.
  1318. */
  1319. static void perf_event_groups_init(struct perf_event_groups *groups)
  1320. {
  1321. groups->tree = RB_ROOT;
  1322. groups->index = 0;
  1323. }
  1324. static inline struct cgroup *event_cgroup(const struct perf_event *event)
  1325. {
  1326. struct cgroup *cgroup = NULL;
  1327. #ifdef CONFIG_CGROUP_PERF
  1328. if (event->cgrp)
  1329. cgroup = event->cgrp->css.cgroup;
  1330. #endif
  1331. return cgroup;
  1332. }
  1333. /*
  1334. * Compare function for event groups;
  1335. *
  1336. * Implements complex key that first sorts by CPU and then by virtual index
  1337. * which provides ordering when rotating groups for the same CPU.
  1338. */
  1339. static __always_inline int
  1340. perf_event_groups_cmp(const int left_cpu, const struct cgroup *left_cgroup,
  1341. const u64 left_group_index, const struct perf_event *right)
  1342. {
  1343. if (left_cpu < right->cpu)
  1344. return -1;
  1345. if (left_cpu > right->cpu)
  1346. return 1;
  1347. #ifdef CONFIG_CGROUP_PERF
  1348. {
  1349. const struct cgroup *right_cgroup = event_cgroup(right);
  1350. if (left_cgroup != right_cgroup) {
  1351. if (!left_cgroup) {
  1352. /*
  1353. * Left has no cgroup but right does, no
  1354. * cgroups come first.
  1355. */
  1356. return -1;
  1357. }
  1358. if (!right_cgroup) {
  1359. /*
  1360. * Right has no cgroup but left does, no
  1361. * cgroups come first.
  1362. */
  1363. return 1;
  1364. }
  1365. /* Two dissimilar cgroups, order by id. */
  1366. if (cgroup_id(left_cgroup) < cgroup_id(right_cgroup))
  1367. return -1;
  1368. return 1;
  1369. }
  1370. }
  1371. #endif
  1372. if (left_group_index < right->group_index)
  1373. return -1;
  1374. if (left_group_index > right->group_index)
  1375. return 1;
  1376. return 0;
  1377. }
  1378. #define __node_2_pe(node) \
  1379. rb_entry((node), struct perf_event, group_node)
  1380. static inline bool __group_less(struct rb_node *a, const struct rb_node *b)
  1381. {
  1382. struct perf_event *e = __node_2_pe(a);
  1383. return perf_event_groups_cmp(e->cpu, event_cgroup(e), e->group_index,
  1384. __node_2_pe(b)) < 0;
  1385. }
  1386. struct __group_key {
  1387. int cpu;
  1388. struct cgroup *cgroup;
  1389. };
  1390. static inline int __group_cmp(const void *key, const struct rb_node *node)
  1391. {
  1392. const struct __group_key *a = key;
  1393. const struct perf_event *b = __node_2_pe(node);
  1394. /* partial/subtree match: @cpu, @cgroup; ignore: @group_index */
  1395. return perf_event_groups_cmp(a->cpu, a->cgroup, b->group_index, b);
  1396. }
  1397. /*
  1398. * Insert @event into @groups' tree; using {@event->cpu, ++@groups->index} for
  1399. * key (see perf_event_groups_less). This places it last inside the CPU
  1400. * subtree.
  1401. */
  1402. static void
  1403. perf_event_groups_insert(struct perf_event_groups *groups,
  1404. struct perf_event *event)
  1405. {
  1406. event->group_index = ++groups->index;
  1407. rb_add(&event->group_node, &groups->tree, __group_less);
  1408. }
  1409. /*
  1410. * Helper function to insert event into the pinned or flexible groups.
  1411. */
  1412. static void
  1413. add_event_to_groups(struct perf_event *event, struct perf_event_context *ctx)
  1414. {
  1415. struct perf_event_groups *groups;
  1416. groups = get_event_groups(event, ctx);
  1417. perf_event_groups_insert(groups, event);
  1418. }
  1419. /*
  1420. * Delete a group from a tree.
  1421. */
  1422. static void
  1423. perf_event_groups_delete(struct perf_event_groups *groups,
  1424. struct perf_event *event)
  1425. {
  1426. WARN_ON_ONCE(RB_EMPTY_NODE(&event->group_node) ||
  1427. RB_EMPTY_ROOT(&groups->tree));
  1428. rb_erase(&event->group_node, &groups->tree);
  1429. init_event_group(event);
  1430. }
  1431. /*
  1432. * Helper function to delete event from its groups.
  1433. */
  1434. static void
  1435. del_event_from_groups(struct perf_event *event, struct perf_event_context *ctx)
  1436. {
  1437. struct perf_event_groups *groups;
  1438. groups = get_event_groups(event, ctx);
  1439. perf_event_groups_delete(groups, event);
  1440. }
  1441. /*
  1442. * Get the leftmost event in the cpu/cgroup subtree.
  1443. */
  1444. static struct perf_event *
  1445. perf_event_groups_first(struct perf_event_groups *groups, int cpu,
  1446. struct cgroup *cgrp)
  1447. {
  1448. struct __group_key key = {
  1449. .cpu = cpu,
  1450. .cgroup = cgrp,
  1451. };
  1452. struct rb_node *node;
  1453. node = rb_find_first(&key, &groups->tree, __group_cmp);
  1454. if (node)
  1455. return __node_2_pe(node);
  1456. return NULL;
  1457. }
  1458. /*
  1459. * Like rb_entry_next_safe() for the @cpu subtree.
  1460. */
  1461. static struct perf_event *
  1462. perf_event_groups_next(struct perf_event *event)
  1463. {
  1464. struct __group_key key = {
  1465. .cpu = event->cpu,
  1466. .cgroup = event_cgroup(event),
  1467. };
  1468. struct rb_node *next;
  1469. next = rb_next_match(&key, &event->group_node, __group_cmp);
  1470. if (next)
  1471. return __node_2_pe(next);
  1472. return NULL;
  1473. }
  1474. /*
  1475. * Iterate through the whole groups tree.
  1476. */
  1477. #define perf_event_groups_for_each(event, groups) \
  1478. for (event = rb_entry_safe(rb_first(&((groups)->tree)), \
  1479. typeof(*event), group_node); event; \
  1480. event = rb_entry_safe(rb_next(&event->group_node), \
  1481. typeof(*event), group_node))
  1482. /*
  1483. * Add an event from the lists for its context.
  1484. * Must be called with ctx->mutex and ctx->lock held.
  1485. */
  1486. static void
  1487. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  1488. {
  1489. lockdep_assert_held(&ctx->lock);
  1490. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  1491. event->attach_state |= PERF_ATTACH_CONTEXT;
  1492. event->tstamp = perf_event_time(event);
  1493. /*
  1494. * If we're a stand alone event or group leader, we go to the context
  1495. * list, group events are kept attached to the group so that
  1496. * perf_group_detach can, at all times, locate all siblings.
  1497. */
  1498. if (event->group_leader == event) {
  1499. event->group_caps = event->event_caps;
  1500. add_event_to_groups(event, ctx);
  1501. }
  1502. list_add_rcu(&event->event_entry, &ctx->event_list);
  1503. ctx->nr_events++;
  1504. if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT)
  1505. ctx->nr_user++;
  1506. if (event->attr.inherit_stat)
  1507. ctx->nr_stat++;
  1508. if (event->state > PERF_EVENT_STATE_OFF)
  1509. perf_cgroup_event_enable(event, ctx);
  1510. ctx->generation++;
  1511. }
  1512. /*
  1513. * Initialize event state based on the perf_event_attr::disabled.
  1514. */
  1515. static inline void perf_event__state_init(struct perf_event *event)
  1516. {
  1517. event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
  1518. PERF_EVENT_STATE_INACTIVE;
  1519. }
  1520. static int __perf_event_read_size(u64 read_format, int nr_siblings)
  1521. {
  1522. int entry = sizeof(u64); /* value */
  1523. int size = 0;
  1524. int nr = 1;
  1525. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1526. size += sizeof(u64);
  1527. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1528. size += sizeof(u64);
  1529. if (read_format & PERF_FORMAT_ID)
  1530. entry += sizeof(u64);
  1531. if (read_format & PERF_FORMAT_LOST)
  1532. entry += sizeof(u64);
  1533. if (read_format & PERF_FORMAT_GROUP) {
  1534. nr += nr_siblings;
  1535. size += sizeof(u64);
  1536. }
  1537. /*
  1538. * Since perf_event_validate_size() limits this to 16k and inhibits
  1539. * adding more siblings, this will never overflow.
  1540. */
  1541. return size + nr * entry;
  1542. }
  1543. static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
  1544. {
  1545. struct perf_sample_data *data;
  1546. u16 size = 0;
  1547. if (sample_type & PERF_SAMPLE_IP)
  1548. size += sizeof(data->ip);
  1549. if (sample_type & PERF_SAMPLE_ADDR)
  1550. size += sizeof(data->addr);
  1551. if (sample_type & PERF_SAMPLE_PERIOD)
  1552. size += sizeof(data->period);
  1553. if (sample_type & PERF_SAMPLE_WEIGHT_TYPE)
  1554. size += sizeof(data->weight.full);
  1555. if (sample_type & PERF_SAMPLE_READ)
  1556. size += event->read_size;
  1557. if (sample_type & PERF_SAMPLE_DATA_SRC)
  1558. size += sizeof(data->data_src.val);
  1559. if (sample_type & PERF_SAMPLE_TRANSACTION)
  1560. size += sizeof(data->txn);
  1561. if (sample_type & PERF_SAMPLE_PHYS_ADDR)
  1562. size += sizeof(data->phys_addr);
  1563. if (sample_type & PERF_SAMPLE_CGROUP)
  1564. size += sizeof(data->cgroup);
  1565. if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)
  1566. size += sizeof(data->data_page_size);
  1567. if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)
  1568. size += sizeof(data->code_page_size);
  1569. event->header_size = size;
  1570. }
  1571. /*
  1572. * Called at perf_event creation and when events are attached/detached from a
  1573. * group.
  1574. */
  1575. static void perf_event__header_size(struct perf_event *event)
  1576. {
  1577. event->read_size =
  1578. __perf_event_read_size(event->attr.read_format,
  1579. event->group_leader->nr_siblings);
  1580. __perf_event_header_size(event, event->attr.sample_type);
  1581. }
  1582. static void perf_event__id_header_size(struct perf_event *event)
  1583. {
  1584. struct perf_sample_data *data;
  1585. u64 sample_type = event->attr.sample_type;
  1586. u16 size = 0;
  1587. if (sample_type & PERF_SAMPLE_TID)
  1588. size += sizeof(data->tid_entry);
  1589. if (sample_type & PERF_SAMPLE_TIME)
  1590. size += sizeof(data->time);
  1591. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  1592. size += sizeof(data->id);
  1593. if (sample_type & PERF_SAMPLE_ID)
  1594. size += sizeof(data->id);
  1595. if (sample_type & PERF_SAMPLE_STREAM_ID)
  1596. size += sizeof(data->stream_id);
  1597. if (sample_type & PERF_SAMPLE_CPU)
  1598. size += sizeof(data->cpu_entry);
  1599. event->id_header_size = size;
  1600. }
  1601. /*
  1602. * Check that adding an event to the group does not result in anybody
  1603. * overflowing the 64k event limit imposed by the output buffer.
  1604. *
  1605. * Specifically, check that the read_size for the event does not exceed 16k,
  1606. * read_size being the one term that grows with groups size. Since read_size
  1607. * depends on per-event read_format, also (re)check the existing events.
  1608. *
  1609. * This leaves 48k for the constant size fields and things like callchains,
  1610. * branch stacks and register sets.
  1611. */
  1612. static bool perf_event_validate_size(struct perf_event *event)
  1613. {
  1614. struct perf_event *sibling, *group_leader = event->group_leader;
  1615. if (__perf_event_read_size(event->attr.read_format,
  1616. group_leader->nr_siblings + 1) > 16*1024)
  1617. return false;
  1618. if (__perf_event_read_size(group_leader->attr.read_format,
  1619. group_leader->nr_siblings + 1) > 16*1024)
  1620. return false;
  1621. for_each_sibling_event(sibling, group_leader) {
  1622. if (__perf_event_read_size(sibling->attr.read_format,
  1623. group_leader->nr_siblings + 1) > 16*1024)
  1624. return false;
  1625. }
  1626. return true;
  1627. }
  1628. static void perf_group_attach(struct perf_event *event)
  1629. {
  1630. struct perf_event *group_leader = event->group_leader, *pos;
  1631. lockdep_assert_held(&event->ctx->lock);
  1632. /*
  1633. * We can have double attach due to group movement in perf_event_open.
  1634. */
  1635. if (event->attach_state & PERF_ATTACH_GROUP)
  1636. return;
  1637. event->attach_state |= PERF_ATTACH_GROUP;
  1638. if (group_leader == event)
  1639. return;
  1640. WARN_ON_ONCE(group_leader->ctx != event->ctx);
  1641. group_leader->group_caps &= event->event_caps;
  1642. list_add_tail(&event->sibling_list, &group_leader->sibling_list);
  1643. group_leader->nr_siblings++;
  1644. group_leader->group_generation++;
  1645. perf_event__header_size(group_leader);
  1646. for_each_sibling_event(pos, group_leader)
  1647. perf_event__header_size(pos);
  1648. }
  1649. /*
  1650. * Remove an event from the lists for its context.
  1651. * Must be called with ctx->mutex and ctx->lock held.
  1652. */
  1653. static void
  1654. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  1655. {
  1656. WARN_ON_ONCE(event->ctx != ctx);
  1657. lockdep_assert_held(&ctx->lock);
  1658. /*
  1659. * We can have double detach due to exit/hot-unplug + close.
  1660. */
  1661. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  1662. return;
  1663. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  1664. ctx->nr_events--;
  1665. if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT)
  1666. ctx->nr_user--;
  1667. if (event->attr.inherit_stat)
  1668. ctx->nr_stat--;
  1669. list_del_rcu(&event->event_entry);
  1670. if (event->group_leader == event)
  1671. del_event_from_groups(event, ctx);
  1672. /*
  1673. * If event was in error state, then keep it
  1674. * that way, otherwise bogus counts will be
  1675. * returned on read(). The only way to get out
  1676. * of error state is by explicit re-enabling
  1677. * of the event
  1678. */
  1679. if (event->state > PERF_EVENT_STATE_OFF) {
  1680. perf_cgroup_event_disable(event, ctx);
  1681. perf_event_set_state(event, PERF_EVENT_STATE_OFF);
  1682. }
  1683. ctx->generation++;
  1684. }
  1685. static int
  1686. perf_aux_output_match(struct perf_event *event, struct perf_event *aux_event)
  1687. {
  1688. if (!has_aux(aux_event))
  1689. return 0;
  1690. if (!event->pmu->aux_output_match)
  1691. return 0;
  1692. return event->pmu->aux_output_match(aux_event);
  1693. }
  1694. static void put_event(struct perf_event *event);
  1695. static void event_sched_out(struct perf_event *event,
  1696. struct perf_cpu_context *cpuctx,
  1697. struct perf_event_context *ctx);
  1698. static void perf_put_aux_event(struct perf_event *event)
  1699. {
  1700. struct perf_event_context *ctx = event->ctx;
  1701. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1702. struct perf_event *iter;
  1703. /*
  1704. * If event uses aux_event tear down the link
  1705. */
  1706. if (event->aux_event) {
  1707. iter = event->aux_event;
  1708. event->aux_event = NULL;
  1709. put_event(iter);
  1710. return;
  1711. }
  1712. /*
  1713. * If the event is an aux_event, tear down all links to
  1714. * it from other events.
  1715. */
  1716. for_each_sibling_event(iter, event->group_leader) {
  1717. if (iter->aux_event != event)
  1718. continue;
  1719. iter->aux_event = NULL;
  1720. put_event(event);
  1721. /*
  1722. * If it's ACTIVE, schedule it out and put it into ERROR
  1723. * state so that we don't try to schedule it again. Note
  1724. * that perf_event_enable() will clear the ERROR status.
  1725. */
  1726. event_sched_out(iter, cpuctx, ctx);
  1727. perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
  1728. }
  1729. }
  1730. static bool perf_need_aux_event(struct perf_event *event)
  1731. {
  1732. return !!event->attr.aux_output || !!event->attr.aux_sample_size;
  1733. }
  1734. static int perf_get_aux_event(struct perf_event *event,
  1735. struct perf_event *group_leader)
  1736. {
  1737. /*
  1738. * Our group leader must be an aux event if we want to be
  1739. * an aux_output. This way, the aux event will precede its
  1740. * aux_output events in the group, and therefore will always
  1741. * schedule first.
  1742. */
  1743. if (!group_leader)
  1744. return 0;
  1745. /*
  1746. * aux_output and aux_sample_size are mutually exclusive.
  1747. */
  1748. if (event->attr.aux_output && event->attr.aux_sample_size)
  1749. return 0;
  1750. if (event->attr.aux_output &&
  1751. !perf_aux_output_match(event, group_leader))
  1752. return 0;
  1753. if (event->attr.aux_sample_size && !group_leader->pmu->snapshot_aux)
  1754. return 0;
  1755. if (!atomic_long_inc_not_zero(&group_leader->refcount))
  1756. return 0;
  1757. /*
  1758. * Link aux_outputs to their aux event; this is undone in
  1759. * perf_group_detach() by perf_put_aux_event(). When the
  1760. * group in torn down, the aux_output events loose their
  1761. * link to the aux_event and can't schedule any more.
  1762. */
  1763. event->aux_event = group_leader;
  1764. return 1;
  1765. }
  1766. static inline struct list_head *get_event_list(struct perf_event *event)
  1767. {
  1768. struct perf_event_context *ctx = event->ctx;
  1769. return event->attr.pinned ? &ctx->pinned_active : &ctx->flexible_active;
  1770. }
  1771. /*
  1772. * Events that have PERF_EV_CAP_SIBLING require being part of a group and
  1773. * cannot exist on their own, schedule them out and move them into the ERROR
  1774. * state. Also see _perf_event_enable(), it will not be able to recover
  1775. * this ERROR state.
  1776. */
  1777. static inline void perf_remove_sibling_event(struct perf_event *event)
  1778. {
  1779. struct perf_event_context *ctx = event->ctx;
  1780. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1781. event_sched_out(event, cpuctx, ctx);
  1782. perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
  1783. }
  1784. static void perf_group_detach(struct perf_event *event)
  1785. {
  1786. struct perf_event *leader = event->group_leader;
  1787. struct perf_event *sibling, *tmp;
  1788. struct perf_event_context *ctx = event->ctx;
  1789. lockdep_assert_held(&ctx->lock);
  1790. /*
  1791. * We can have double detach due to exit/hot-unplug + close.
  1792. */
  1793. if (!(event->attach_state & PERF_ATTACH_GROUP))
  1794. return;
  1795. event->attach_state &= ~PERF_ATTACH_GROUP;
  1796. perf_put_aux_event(event);
  1797. /*
  1798. * If this is a sibling, remove it from its group.
  1799. */
  1800. if (leader != event) {
  1801. list_del_init(&event->sibling_list);
  1802. event->group_leader->nr_siblings--;
  1803. event->group_leader->group_generation++;
  1804. goto out;
  1805. }
  1806. /*
  1807. * If this was a group event with sibling events then
  1808. * upgrade the siblings to singleton events by adding them
  1809. * to whatever list we are on.
  1810. */
  1811. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) {
  1812. if (sibling->event_caps & PERF_EV_CAP_SIBLING)
  1813. perf_remove_sibling_event(sibling);
  1814. sibling->group_leader = sibling;
  1815. list_del_init(&sibling->sibling_list);
  1816. /* Inherit group flags from the previous leader */
  1817. sibling->group_caps = event->group_caps;
  1818. if (sibling->attach_state & PERF_ATTACH_CONTEXT) {
  1819. add_event_to_groups(sibling, event->ctx);
  1820. if (sibling->state == PERF_EVENT_STATE_ACTIVE)
  1821. list_add_tail(&sibling->active_list, get_event_list(sibling));
  1822. }
  1823. WARN_ON_ONCE(sibling->ctx != event->ctx);
  1824. }
  1825. out:
  1826. for_each_sibling_event(tmp, leader)
  1827. perf_event__header_size(tmp);
  1828. perf_event__header_size(leader);
  1829. }
  1830. static void sync_child_event(struct perf_event *child_event);
  1831. static void perf_child_detach(struct perf_event *event)
  1832. {
  1833. struct perf_event *parent_event = event->parent;
  1834. if (!(event->attach_state & PERF_ATTACH_CHILD))
  1835. return;
  1836. event->attach_state &= ~PERF_ATTACH_CHILD;
  1837. if (WARN_ON_ONCE(!parent_event))
  1838. return;
  1839. lockdep_assert_held(&parent_event->child_mutex);
  1840. sync_child_event(event);
  1841. list_del_init(&event->child_list);
  1842. }
  1843. static bool is_orphaned_event(struct perf_event *event)
  1844. {
  1845. return event->state == PERF_EVENT_STATE_DEAD;
  1846. }
  1847. static inline int __pmu_filter_match(struct perf_event *event)
  1848. {
  1849. struct pmu *pmu = event->pmu;
  1850. return pmu->filter_match ? pmu->filter_match(event) : 1;
  1851. }
  1852. /*
  1853. * Check whether we should attempt to schedule an event group based on
  1854. * PMU-specific filtering. An event group can consist of HW and SW events,
  1855. * potentially with a SW leader, so we must check all the filters, to
  1856. * determine whether a group is schedulable:
  1857. */
  1858. static inline int pmu_filter_match(struct perf_event *event)
  1859. {
  1860. struct perf_event *sibling;
  1861. unsigned long flags;
  1862. int ret = 1;
  1863. if (!__pmu_filter_match(event))
  1864. return 0;
  1865. local_irq_save(flags);
  1866. for_each_sibling_event(sibling, event) {
  1867. if (!__pmu_filter_match(sibling)) {
  1868. ret = 0;
  1869. break;
  1870. }
  1871. }
  1872. local_irq_restore(flags);
  1873. return ret;
  1874. }
  1875. static inline int
  1876. event_filter_match(struct perf_event *event)
  1877. {
  1878. return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
  1879. perf_cgroup_match(event) && pmu_filter_match(event);
  1880. }
  1881. static void
  1882. event_sched_out(struct perf_event *event,
  1883. struct perf_cpu_context *cpuctx,
  1884. struct perf_event_context *ctx)
  1885. {
  1886. enum perf_event_state state = PERF_EVENT_STATE_INACTIVE;
  1887. WARN_ON_ONCE(event->ctx != ctx);
  1888. lockdep_assert_held(&ctx->lock);
  1889. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1890. return;
  1891. /*
  1892. * Asymmetry; we only schedule events _IN_ through ctx_sched_in(), but
  1893. * we can schedule events _OUT_ individually through things like
  1894. * __perf_remove_from_context().
  1895. */
  1896. list_del_init(&event->active_list);
  1897. perf_pmu_disable(event->pmu);
  1898. event->pmu->del(event, 0);
  1899. event->oncpu = -1;
  1900. if (event->pending_disable) {
  1901. event->pending_disable = 0;
  1902. perf_cgroup_event_disable(event, ctx);
  1903. state = PERF_EVENT_STATE_OFF;
  1904. }
  1905. if (event->pending_sigtrap) {
  1906. bool dec = true;
  1907. event->pending_sigtrap = 0;
  1908. if (state != PERF_EVENT_STATE_OFF &&
  1909. !event->pending_work) {
  1910. event->pending_work = 1;
  1911. dec = false;
  1912. WARN_ON_ONCE(!atomic_long_inc_not_zero(&event->refcount));
  1913. task_work_add(current, &event->pending_task, TWA_RESUME);
  1914. }
  1915. if (dec)
  1916. local_dec(&event->ctx->nr_pending);
  1917. }
  1918. perf_event_set_state(event, state);
  1919. if (!is_software_event(event))
  1920. cpuctx->active_oncpu--;
  1921. if (!--ctx->nr_active)
  1922. perf_event_ctx_deactivate(ctx);
  1923. if (event->attr.freq && event->attr.sample_freq)
  1924. ctx->nr_freq--;
  1925. if (event->attr.exclusive || !cpuctx->active_oncpu)
  1926. cpuctx->exclusive = 0;
  1927. perf_pmu_enable(event->pmu);
  1928. }
  1929. static void
  1930. group_sched_out(struct perf_event *group_event,
  1931. struct perf_cpu_context *cpuctx,
  1932. struct perf_event_context *ctx)
  1933. {
  1934. struct perf_event *event;
  1935. if (group_event->state != PERF_EVENT_STATE_ACTIVE)
  1936. return;
  1937. perf_pmu_disable(ctx->pmu);
  1938. event_sched_out(group_event, cpuctx, ctx);
  1939. /*
  1940. * Schedule out siblings (if any):
  1941. */
  1942. for_each_sibling_event(event, group_event)
  1943. event_sched_out(event, cpuctx, ctx);
  1944. perf_pmu_enable(ctx->pmu);
  1945. }
  1946. #define DETACH_GROUP 0x01UL
  1947. #define DETACH_CHILD 0x02UL
  1948. #define DETACH_DEAD 0x04UL
  1949. /*
  1950. * Cross CPU call to remove a performance event
  1951. *
  1952. * We disable the event on the hardware level first. After that we
  1953. * remove it from the context list.
  1954. */
  1955. static void
  1956. __perf_remove_from_context(struct perf_event *event,
  1957. struct perf_cpu_context *cpuctx,
  1958. struct perf_event_context *ctx,
  1959. void *info)
  1960. {
  1961. unsigned long flags = (unsigned long)info;
  1962. if (ctx->is_active & EVENT_TIME) {
  1963. update_context_time(ctx);
  1964. update_cgrp_time_from_cpuctx(cpuctx, false);
  1965. }
  1966. /*
  1967. * Ensure event_sched_out() switches to OFF, at the very least
  1968. * this avoids raising perf_pending_task() at this time.
  1969. */
  1970. if (flags & DETACH_DEAD)
  1971. event->pending_disable = 1;
  1972. event_sched_out(event, cpuctx, ctx);
  1973. if (flags & DETACH_GROUP)
  1974. perf_group_detach(event);
  1975. if (flags & DETACH_CHILD)
  1976. perf_child_detach(event);
  1977. list_del_event(event, ctx);
  1978. if (flags & DETACH_DEAD)
  1979. event->state = PERF_EVENT_STATE_DEAD;
  1980. if (!ctx->nr_events && ctx->is_active) {
  1981. if (ctx == &cpuctx->ctx)
  1982. update_cgrp_time_from_cpuctx(cpuctx, true);
  1983. ctx->is_active = 0;
  1984. ctx->rotate_necessary = 0;
  1985. if (ctx->task) {
  1986. WARN_ON_ONCE(cpuctx->task_ctx != ctx);
  1987. cpuctx->task_ctx = NULL;
  1988. }
  1989. }
  1990. }
  1991. /*
  1992. * Remove the event from a task's (or a CPU's) list of events.
  1993. *
  1994. * If event->ctx is a cloned context, callers must make sure that
  1995. * every task struct that event->ctx->task could possibly point to
  1996. * remains valid. This is OK when called from perf_release since
  1997. * that only calls us on the top-level context, which can't be a clone.
  1998. * When called from perf_event_exit_task, it's OK because the
  1999. * context has been detached from its task.
  2000. */
  2001. static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
  2002. {
  2003. struct perf_event_context *ctx = event->ctx;
  2004. lockdep_assert_held(&ctx->mutex);
  2005. /*
  2006. * Because of perf_event_exit_task(), perf_remove_from_context() ought
  2007. * to work in the face of TASK_TOMBSTONE, unlike every other
  2008. * event_function_call() user.
  2009. */
  2010. raw_spin_lock_irq(&ctx->lock);
  2011. /*
  2012. * Cgroup events are per-cpu events, and must IPI because of
  2013. * cgrp_cpuctx_list.
  2014. */
  2015. if (!ctx->is_active && !is_cgroup_event(event)) {
  2016. __perf_remove_from_context(event, __get_cpu_context(ctx),
  2017. ctx, (void *)flags);
  2018. raw_spin_unlock_irq(&ctx->lock);
  2019. return;
  2020. }
  2021. raw_spin_unlock_irq(&ctx->lock);
  2022. event_function_call(event, __perf_remove_from_context, (void *)flags);
  2023. }
  2024. /*
  2025. * Cross CPU call to disable a performance event
  2026. */
  2027. static void __perf_event_disable(struct perf_event *event,
  2028. struct perf_cpu_context *cpuctx,
  2029. struct perf_event_context *ctx,
  2030. void *info)
  2031. {
  2032. if (event->state < PERF_EVENT_STATE_INACTIVE)
  2033. return;
  2034. if (ctx->is_active & EVENT_TIME) {
  2035. update_context_time(ctx);
  2036. update_cgrp_time_from_event(event);
  2037. }
  2038. if (event == event->group_leader)
  2039. group_sched_out(event, cpuctx, ctx);
  2040. else
  2041. event_sched_out(event, cpuctx, ctx);
  2042. perf_event_set_state(event, PERF_EVENT_STATE_OFF);
  2043. perf_cgroup_event_disable(event, ctx);
  2044. }
  2045. /*
  2046. * Disable an event.
  2047. *
  2048. * If event->ctx is a cloned context, callers must make sure that
  2049. * every task struct that event->ctx->task could possibly point to
  2050. * remains valid. This condition is satisfied when called through
  2051. * perf_event_for_each_child or perf_event_for_each because they
  2052. * hold the top-level event's child_mutex, so any descendant that
  2053. * goes to exit will block in perf_event_exit_event().
  2054. *
  2055. * When called from perf_pending_irq it's OK because event->ctx
  2056. * is the current context on this CPU and preemption is disabled,
  2057. * hence we can't get into perf_event_task_sched_out for this context.
  2058. */
  2059. static void _perf_event_disable(struct perf_event *event)
  2060. {
  2061. struct perf_event_context *ctx = event->ctx;
  2062. raw_spin_lock_irq(&ctx->lock);
  2063. if (event->state <= PERF_EVENT_STATE_OFF) {
  2064. raw_spin_unlock_irq(&ctx->lock);
  2065. return;
  2066. }
  2067. raw_spin_unlock_irq(&ctx->lock);
  2068. event_function_call(event, __perf_event_disable, NULL);
  2069. }
  2070. void perf_event_disable_local(struct perf_event *event)
  2071. {
  2072. event_function_local(event, __perf_event_disable, NULL);
  2073. }
  2074. /*
  2075. * Strictly speaking kernel users cannot create groups and therefore this
  2076. * interface does not need the perf_event_ctx_lock() magic.
  2077. */
  2078. void perf_event_disable(struct perf_event *event)
  2079. {
  2080. struct perf_event_context *ctx;
  2081. ctx = perf_event_ctx_lock(event);
  2082. _perf_event_disable(event);
  2083. perf_event_ctx_unlock(event, ctx);
  2084. }
  2085. EXPORT_SYMBOL_GPL(perf_event_disable);
  2086. void perf_event_disable_inatomic(struct perf_event *event)
  2087. {
  2088. event->pending_disable = 1;
  2089. irq_work_queue(&event->pending_irq);
  2090. }
  2091. #define MAX_INTERRUPTS (~0ULL)
  2092. static void perf_log_throttle(struct perf_event *event, int enable);
  2093. static void perf_log_itrace_start(struct perf_event *event);
  2094. static int
  2095. event_sched_in(struct perf_event *event,
  2096. struct perf_cpu_context *cpuctx,
  2097. struct perf_event_context *ctx)
  2098. {
  2099. int ret = 0;
  2100. WARN_ON_ONCE(event->ctx != ctx);
  2101. lockdep_assert_held(&ctx->lock);
  2102. if (event->state <= PERF_EVENT_STATE_OFF)
  2103. return 0;
  2104. WRITE_ONCE(event->oncpu, smp_processor_id());
  2105. /*
  2106. * Order event::oncpu write to happen before the ACTIVE state is
  2107. * visible. This allows perf_event_{stop,read}() to observe the correct
  2108. * ->oncpu if it sees ACTIVE.
  2109. */
  2110. smp_wmb();
  2111. perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE);
  2112. /*
  2113. * Unthrottle events, since we scheduled we might have missed several
  2114. * ticks already, also for a heavily scheduling task there is little
  2115. * guarantee it'll get a tick in a timely manner.
  2116. */
  2117. if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
  2118. perf_log_throttle(event, 1);
  2119. event->hw.interrupts = 0;
  2120. }
  2121. perf_pmu_disable(event->pmu);
  2122. perf_log_itrace_start(event);
  2123. if (event->pmu->add(event, PERF_EF_START)) {
  2124. perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
  2125. event->oncpu = -1;
  2126. ret = -EAGAIN;
  2127. goto out;
  2128. }
  2129. if (!is_software_event(event))
  2130. cpuctx->active_oncpu++;
  2131. if (!ctx->nr_active++)
  2132. perf_event_ctx_activate(ctx);
  2133. if (event->attr.freq && event->attr.sample_freq)
  2134. ctx->nr_freq++;
  2135. if (event->attr.exclusive)
  2136. cpuctx->exclusive = 1;
  2137. out:
  2138. perf_pmu_enable(event->pmu);
  2139. return ret;
  2140. }
  2141. static int
  2142. group_sched_in(struct perf_event *group_event,
  2143. struct perf_cpu_context *cpuctx,
  2144. struct perf_event_context *ctx)
  2145. {
  2146. struct perf_event *event, *partial_group = NULL;
  2147. struct pmu *pmu = ctx->pmu;
  2148. if (group_event->state == PERF_EVENT_STATE_OFF)
  2149. return 0;
  2150. pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
  2151. if (event_sched_in(group_event, cpuctx, ctx))
  2152. goto error;
  2153. /*
  2154. * Schedule in siblings as one group (if any):
  2155. */
  2156. for_each_sibling_event(event, group_event) {
  2157. if (event_sched_in(event, cpuctx, ctx)) {
  2158. partial_group = event;
  2159. goto group_error;
  2160. }
  2161. }
  2162. if (!pmu->commit_txn(pmu))
  2163. return 0;
  2164. group_error:
  2165. /*
  2166. * Groups can be scheduled in as one unit only, so undo any
  2167. * partial group before returning:
  2168. * The events up to the failed event are scheduled out normally.
  2169. */
  2170. for_each_sibling_event(event, group_event) {
  2171. if (event == partial_group)
  2172. break;
  2173. event_sched_out(event, cpuctx, ctx);
  2174. }
  2175. event_sched_out(group_event, cpuctx, ctx);
  2176. error:
  2177. pmu->cancel_txn(pmu);
  2178. return -EAGAIN;
  2179. }
  2180. /*
  2181. * Work out whether we can put this event group on the CPU now.
  2182. */
  2183. static int group_can_go_on(struct perf_event *event,
  2184. struct perf_cpu_context *cpuctx,
  2185. int can_add_hw)
  2186. {
  2187. /*
  2188. * Groups consisting entirely of software events can always go on.
  2189. */
  2190. if (event->group_caps & PERF_EV_CAP_SOFTWARE)
  2191. return 1;
  2192. /*
  2193. * If an exclusive group is already on, no other hardware
  2194. * events can go on.
  2195. */
  2196. if (cpuctx->exclusive)
  2197. return 0;
  2198. /*
  2199. * If this group is exclusive and there are already
  2200. * events on the CPU, it can't go on.
  2201. */
  2202. if (event->attr.exclusive && !list_empty(get_event_list(event)))
  2203. return 0;
  2204. /*
  2205. * Otherwise, try to add it if all previous groups were able
  2206. * to go on.
  2207. */
  2208. return can_add_hw;
  2209. }
  2210. static void add_event_to_ctx(struct perf_event *event,
  2211. struct perf_event_context *ctx)
  2212. {
  2213. list_add_event(event, ctx);
  2214. perf_group_attach(event);
  2215. }
  2216. static void ctx_sched_out(struct perf_event_context *ctx,
  2217. struct perf_cpu_context *cpuctx,
  2218. enum event_type_t event_type);
  2219. static void
  2220. ctx_sched_in(struct perf_event_context *ctx,
  2221. struct perf_cpu_context *cpuctx,
  2222. enum event_type_t event_type);
  2223. static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
  2224. struct perf_event_context *ctx,
  2225. enum event_type_t event_type)
  2226. {
  2227. if (!cpuctx->task_ctx)
  2228. return;
  2229. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  2230. return;
  2231. ctx_sched_out(ctx, cpuctx, event_type);
  2232. }
  2233. static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
  2234. struct perf_event_context *ctx)
  2235. {
  2236. cpu_ctx_sched_in(cpuctx, EVENT_PINNED);
  2237. if (ctx)
  2238. ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
  2239. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  2240. if (ctx)
  2241. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
  2242. }
  2243. /*
  2244. * We want to maintain the following priority of scheduling:
  2245. * - CPU pinned (EVENT_CPU | EVENT_PINNED)
  2246. * - task pinned (EVENT_PINNED)
  2247. * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE)
  2248. * - task flexible (EVENT_FLEXIBLE).
  2249. *
  2250. * In order to avoid unscheduling and scheduling back in everything every
  2251. * time an event is added, only do it for the groups of equal priority and
  2252. * below.
  2253. *
  2254. * This can be called after a batch operation on task events, in which case
  2255. * event_type is a bit mask of the types of events involved. For CPU events,
  2256. * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE.
  2257. */
  2258. static void ctx_resched(struct perf_cpu_context *cpuctx,
  2259. struct perf_event_context *task_ctx,
  2260. enum event_type_t event_type)
  2261. {
  2262. enum event_type_t ctx_event_type;
  2263. bool cpu_event = !!(event_type & EVENT_CPU);
  2264. /*
  2265. * If pinned groups are involved, flexible groups also need to be
  2266. * scheduled out.
  2267. */
  2268. if (event_type & EVENT_PINNED)
  2269. event_type |= EVENT_FLEXIBLE;
  2270. ctx_event_type = event_type & EVENT_ALL;
  2271. perf_pmu_disable(cpuctx->ctx.pmu);
  2272. if (task_ctx)
  2273. task_ctx_sched_out(cpuctx, task_ctx, event_type);
  2274. /*
  2275. * Decide which cpu ctx groups to schedule out based on the types
  2276. * of events that caused rescheduling:
  2277. * - EVENT_CPU: schedule out corresponding groups;
  2278. * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups;
  2279. * - otherwise, do nothing more.
  2280. */
  2281. if (cpu_event)
  2282. cpu_ctx_sched_out(cpuctx, ctx_event_type);
  2283. else if (ctx_event_type & EVENT_PINNED)
  2284. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2285. perf_event_sched_in(cpuctx, task_ctx);
  2286. perf_pmu_enable(cpuctx->ctx.pmu);
  2287. }
  2288. void perf_pmu_resched(struct pmu *pmu)
  2289. {
  2290. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  2291. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  2292. perf_ctx_lock(cpuctx, task_ctx);
  2293. ctx_resched(cpuctx, task_ctx, EVENT_ALL|EVENT_CPU);
  2294. perf_ctx_unlock(cpuctx, task_ctx);
  2295. }
  2296. /*
  2297. * Cross CPU call to install and enable a performance event
  2298. *
  2299. * Very similar to remote_function() + event_function() but cannot assume that
  2300. * things like ctx->is_active and cpuctx->task_ctx are set.
  2301. */
  2302. static int __perf_install_in_context(void *info)
  2303. {
  2304. struct perf_event *event = info;
  2305. struct perf_event_context *ctx = event->ctx;
  2306. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  2307. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  2308. bool reprogram = true;
  2309. int ret = 0;
  2310. raw_spin_lock(&cpuctx->ctx.lock);
  2311. if (ctx->task) {
  2312. raw_spin_lock(&ctx->lock);
  2313. task_ctx = ctx;
  2314. reprogram = (ctx->task == current);
  2315. /*
  2316. * If the task is running, it must be running on this CPU,
  2317. * otherwise we cannot reprogram things.
  2318. *
  2319. * If its not running, we don't care, ctx->lock will
  2320. * serialize against it becoming runnable.
  2321. */
  2322. if (task_curr(ctx->task) && !reprogram) {
  2323. ret = -ESRCH;
  2324. goto unlock;
  2325. }
  2326. WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
  2327. } else if (task_ctx) {
  2328. raw_spin_lock(&task_ctx->lock);
  2329. }
  2330. #ifdef CONFIG_CGROUP_PERF
  2331. if (event->state > PERF_EVENT_STATE_OFF && is_cgroup_event(event)) {
  2332. /*
  2333. * If the current cgroup doesn't match the event's
  2334. * cgroup, we should not try to schedule it.
  2335. */
  2336. struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx);
  2337. reprogram = cgroup_is_descendant(cgrp->css.cgroup,
  2338. event->cgrp->css.cgroup);
  2339. }
  2340. #endif
  2341. if (reprogram) {
  2342. ctx_sched_out(ctx, cpuctx, EVENT_TIME);
  2343. add_event_to_ctx(event, ctx);
  2344. ctx_resched(cpuctx, task_ctx, get_event_type(event));
  2345. } else {
  2346. add_event_to_ctx(event, ctx);
  2347. }
  2348. unlock:
  2349. perf_ctx_unlock(cpuctx, task_ctx);
  2350. return ret;
  2351. }
  2352. static bool exclusive_event_installable(struct perf_event *event,
  2353. struct perf_event_context *ctx);
  2354. /*
  2355. * Attach a performance event to a context.
  2356. *
  2357. * Very similar to event_function_call, see comment there.
  2358. */
  2359. static void
  2360. perf_install_in_context(struct perf_event_context *ctx,
  2361. struct perf_event *event,
  2362. int cpu)
  2363. {
  2364. struct task_struct *task = READ_ONCE(ctx->task);
  2365. lockdep_assert_held(&ctx->mutex);
  2366. WARN_ON_ONCE(!exclusive_event_installable(event, ctx));
  2367. if (event->cpu != -1)
  2368. event->cpu = cpu;
  2369. /*
  2370. * Ensures that if we can observe event->ctx, both the event and ctx
  2371. * will be 'complete'. See perf_iterate_sb_cpu().
  2372. */
  2373. smp_store_release(&event->ctx, ctx);
  2374. /*
  2375. * perf_event_attr::disabled events will not run and can be initialized
  2376. * without IPI. Except when this is the first event for the context, in
  2377. * that case we need the magic of the IPI to set ctx->is_active.
  2378. * Similarly, cgroup events for the context also needs the IPI to
  2379. * manipulate the cgrp_cpuctx_list.
  2380. *
  2381. * The IOC_ENABLE that is sure to follow the creation of a disabled
  2382. * event will issue the IPI and reprogram the hardware.
  2383. */
  2384. if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF &&
  2385. ctx->nr_events && !is_cgroup_event(event)) {
  2386. raw_spin_lock_irq(&ctx->lock);
  2387. if (ctx->task == TASK_TOMBSTONE) {
  2388. raw_spin_unlock_irq(&ctx->lock);
  2389. return;
  2390. }
  2391. add_event_to_ctx(event, ctx);
  2392. raw_spin_unlock_irq(&ctx->lock);
  2393. return;
  2394. }
  2395. if (!task) {
  2396. cpu_function_call(cpu, __perf_install_in_context, event);
  2397. return;
  2398. }
  2399. /*
  2400. * Should not happen, we validate the ctx is still alive before calling.
  2401. */
  2402. if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
  2403. return;
  2404. /*
  2405. * Installing events is tricky because we cannot rely on ctx->is_active
  2406. * to be set in case this is the nr_events 0 -> 1 transition.
  2407. *
  2408. * Instead we use task_curr(), which tells us if the task is running.
  2409. * However, since we use task_curr() outside of rq::lock, we can race
  2410. * against the actual state. This means the result can be wrong.
  2411. *
  2412. * If we get a false positive, we retry, this is harmless.
  2413. *
  2414. * If we get a false negative, things are complicated. If we are after
  2415. * perf_event_context_sched_in() ctx::lock will serialize us, and the
  2416. * value must be correct. If we're before, it doesn't matter since
  2417. * perf_event_context_sched_in() will program the counter.
  2418. *
  2419. * However, this hinges on the remote context switch having observed
  2420. * our task->perf_event_ctxp[] store, such that it will in fact take
  2421. * ctx::lock in perf_event_context_sched_in().
  2422. *
  2423. * We do this by task_function_call(), if the IPI fails to hit the task
  2424. * we know any future context switch of task must see the
  2425. * perf_event_ctpx[] store.
  2426. */
  2427. /*
  2428. * This smp_mb() orders the task->perf_event_ctxp[] store with the
  2429. * task_cpu() load, such that if the IPI then does not find the task
  2430. * running, a future context switch of that task must observe the
  2431. * store.
  2432. */
  2433. smp_mb();
  2434. again:
  2435. if (!task_function_call(task, __perf_install_in_context, event))
  2436. return;
  2437. raw_spin_lock_irq(&ctx->lock);
  2438. task = ctx->task;
  2439. if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
  2440. /*
  2441. * Cannot happen because we already checked above (which also
  2442. * cannot happen), and we hold ctx->mutex, which serializes us
  2443. * against perf_event_exit_task_context().
  2444. */
  2445. raw_spin_unlock_irq(&ctx->lock);
  2446. return;
  2447. }
  2448. /*
  2449. * If the task is not running, ctx->lock will avoid it becoming so,
  2450. * thus we can safely install the event.
  2451. */
  2452. if (task_curr(task)) {
  2453. raw_spin_unlock_irq(&ctx->lock);
  2454. goto again;
  2455. }
  2456. add_event_to_ctx(event, ctx);
  2457. raw_spin_unlock_irq(&ctx->lock);
  2458. }
  2459. /*
  2460. * Cross CPU call to enable a performance event
  2461. */
  2462. static void __perf_event_enable(struct perf_event *event,
  2463. struct perf_cpu_context *cpuctx,
  2464. struct perf_event_context *ctx,
  2465. void *info)
  2466. {
  2467. struct perf_event *leader = event->group_leader;
  2468. struct perf_event_context *task_ctx;
  2469. if (event->state >= PERF_EVENT_STATE_INACTIVE ||
  2470. event->state <= PERF_EVENT_STATE_ERROR)
  2471. return;
  2472. if (ctx->is_active)
  2473. ctx_sched_out(ctx, cpuctx, EVENT_TIME);
  2474. perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
  2475. perf_cgroup_event_enable(event, ctx);
  2476. if (!ctx->is_active)
  2477. return;
  2478. if (!event_filter_match(event)) {
  2479. ctx_sched_in(ctx, cpuctx, EVENT_TIME);
  2480. return;
  2481. }
  2482. /*
  2483. * If the event is in a group and isn't the group leader,
  2484. * then don't put it on unless the group is on.
  2485. */
  2486. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
  2487. ctx_sched_in(ctx, cpuctx, EVENT_TIME);
  2488. return;
  2489. }
  2490. task_ctx = cpuctx->task_ctx;
  2491. if (ctx->task)
  2492. WARN_ON_ONCE(task_ctx != ctx);
  2493. ctx_resched(cpuctx, task_ctx, get_event_type(event));
  2494. }
  2495. /*
  2496. * Enable an event.
  2497. *
  2498. * If event->ctx is a cloned context, callers must make sure that
  2499. * every task struct that event->ctx->task could possibly point to
  2500. * remains valid. This condition is satisfied when called through
  2501. * perf_event_for_each_child or perf_event_for_each as described
  2502. * for perf_event_disable.
  2503. */
  2504. static void _perf_event_enable(struct perf_event *event)
  2505. {
  2506. struct perf_event_context *ctx = event->ctx;
  2507. raw_spin_lock_irq(&ctx->lock);
  2508. if (event->state >= PERF_EVENT_STATE_INACTIVE ||
  2509. event->state < PERF_EVENT_STATE_ERROR) {
  2510. out:
  2511. raw_spin_unlock_irq(&ctx->lock);
  2512. return;
  2513. }
  2514. /*
  2515. * If the event is in error state, clear that first.
  2516. *
  2517. * That way, if we see the event in error state below, we know that it
  2518. * has gone back into error state, as distinct from the task having
  2519. * been scheduled away before the cross-call arrived.
  2520. */
  2521. if (event->state == PERF_EVENT_STATE_ERROR) {
  2522. /*
  2523. * Detached SIBLING events cannot leave ERROR state.
  2524. */
  2525. if (event->event_caps & PERF_EV_CAP_SIBLING &&
  2526. event->group_leader == event)
  2527. goto out;
  2528. event->state = PERF_EVENT_STATE_OFF;
  2529. }
  2530. raw_spin_unlock_irq(&ctx->lock);
  2531. event_function_call(event, __perf_event_enable, NULL);
  2532. }
  2533. /*
  2534. * See perf_event_disable();
  2535. */
  2536. void perf_event_enable(struct perf_event *event)
  2537. {
  2538. struct perf_event_context *ctx;
  2539. ctx = perf_event_ctx_lock(event);
  2540. _perf_event_enable(event);
  2541. perf_event_ctx_unlock(event, ctx);
  2542. }
  2543. EXPORT_SYMBOL_GPL(perf_event_enable);
  2544. struct stop_event_data {
  2545. struct perf_event *event;
  2546. unsigned int restart;
  2547. };
  2548. static int __perf_event_stop(void *info)
  2549. {
  2550. struct stop_event_data *sd = info;
  2551. struct perf_event *event = sd->event;
  2552. /* if it's already INACTIVE, do nothing */
  2553. if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
  2554. return 0;
  2555. /* matches smp_wmb() in event_sched_in() */
  2556. smp_rmb();
  2557. /*
  2558. * There is a window with interrupts enabled before we get here,
  2559. * so we need to check again lest we try to stop another CPU's event.
  2560. */
  2561. if (READ_ONCE(event->oncpu) != smp_processor_id())
  2562. return -EAGAIN;
  2563. event->pmu->stop(event, PERF_EF_UPDATE);
  2564. /*
  2565. * May race with the actual stop (through perf_pmu_output_stop()),
  2566. * but it is only used for events with AUX ring buffer, and such
  2567. * events will refuse to restart because of rb::aux_mmap_count==0,
  2568. * see comments in perf_aux_output_begin().
  2569. *
  2570. * Since this is happening on an event-local CPU, no trace is lost
  2571. * while restarting.
  2572. */
  2573. if (sd->restart)
  2574. event->pmu->start(event, 0);
  2575. return 0;
  2576. }
  2577. static int perf_event_stop(struct perf_event *event, int restart)
  2578. {
  2579. struct stop_event_data sd = {
  2580. .event = event,
  2581. .restart = restart,
  2582. };
  2583. int ret = 0;
  2584. do {
  2585. if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
  2586. return 0;
  2587. /* matches smp_wmb() in event_sched_in() */
  2588. smp_rmb();
  2589. /*
  2590. * We only want to restart ACTIVE events, so if the event goes
  2591. * inactive here (event->oncpu==-1), there's nothing more to do;
  2592. * fall through with ret==-ENXIO.
  2593. */
  2594. ret = cpu_function_call(READ_ONCE(event->oncpu),
  2595. __perf_event_stop, &sd);
  2596. } while (ret == -EAGAIN);
  2597. return ret;
  2598. }
  2599. /*
  2600. * In order to contain the amount of racy and tricky in the address filter
  2601. * configuration management, it is a two part process:
  2602. *
  2603. * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
  2604. * we update the addresses of corresponding vmas in
  2605. * event::addr_filter_ranges array and bump the event::addr_filters_gen;
  2606. * (p2) when an event is scheduled in (pmu::add), it calls
  2607. * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
  2608. * if the generation has changed since the previous call.
  2609. *
  2610. * If (p1) happens while the event is active, we restart it to force (p2).
  2611. *
  2612. * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
  2613. * pre-existing mappings, called once when new filters arrive via SET_FILTER
  2614. * ioctl;
  2615. * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
  2616. * registered mapping, called for every new mmap(), with mm::mmap_lock down
  2617. * for reading;
  2618. * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
  2619. * of exec.
  2620. */
  2621. void perf_event_addr_filters_sync(struct perf_event *event)
  2622. {
  2623. struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
  2624. if (!has_addr_filter(event))
  2625. return;
  2626. raw_spin_lock(&ifh->lock);
  2627. if (event->addr_filters_gen != event->hw.addr_filters_gen) {
  2628. event->pmu->addr_filters_sync(event);
  2629. event->hw.addr_filters_gen = event->addr_filters_gen;
  2630. }
  2631. raw_spin_unlock(&ifh->lock);
  2632. }
  2633. EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
  2634. static int _perf_event_refresh(struct perf_event *event, int refresh)
  2635. {
  2636. /*
  2637. * not supported on inherited events
  2638. */
  2639. if (event->attr.inherit || !is_sampling_event(event))
  2640. return -EINVAL;
  2641. atomic_add(refresh, &event->event_limit);
  2642. _perf_event_enable(event);
  2643. return 0;
  2644. }
  2645. /*
  2646. * See perf_event_disable()
  2647. */
  2648. int perf_event_refresh(struct perf_event *event, int refresh)
  2649. {
  2650. struct perf_event_context *ctx;
  2651. int ret;
  2652. ctx = perf_event_ctx_lock(event);
  2653. ret = _perf_event_refresh(event, refresh);
  2654. perf_event_ctx_unlock(event, ctx);
  2655. return ret;
  2656. }
  2657. EXPORT_SYMBOL_GPL(perf_event_refresh);
  2658. static int perf_event_modify_breakpoint(struct perf_event *bp,
  2659. struct perf_event_attr *attr)
  2660. {
  2661. int err;
  2662. _perf_event_disable(bp);
  2663. err = modify_user_hw_breakpoint_check(bp, attr, true);
  2664. if (!bp->attr.disabled)
  2665. _perf_event_enable(bp);
  2666. return err;
  2667. }
  2668. /*
  2669. * Copy event-type-independent attributes that may be modified.
  2670. */
  2671. static void perf_event_modify_copy_attr(struct perf_event_attr *to,
  2672. const struct perf_event_attr *from)
  2673. {
  2674. to->sig_data = from->sig_data;
  2675. }
  2676. static int perf_event_modify_attr(struct perf_event *event,
  2677. struct perf_event_attr *attr)
  2678. {
  2679. int (*func)(struct perf_event *, struct perf_event_attr *);
  2680. struct perf_event *child;
  2681. int err;
  2682. if (event->attr.type != attr->type)
  2683. return -EINVAL;
  2684. switch (event->attr.type) {
  2685. case PERF_TYPE_BREAKPOINT:
  2686. func = perf_event_modify_breakpoint;
  2687. break;
  2688. default:
  2689. /* Place holder for future additions. */
  2690. return -EOPNOTSUPP;
  2691. }
  2692. WARN_ON_ONCE(event->ctx->parent_ctx);
  2693. mutex_lock(&event->child_mutex);
  2694. /*
  2695. * Event-type-independent attributes must be copied before event-type
  2696. * modification, which will validate that final attributes match the
  2697. * source attributes after all relevant attributes have been copied.
  2698. */
  2699. perf_event_modify_copy_attr(&event->attr, attr);
  2700. err = func(event, attr);
  2701. if (err)
  2702. goto out;
  2703. list_for_each_entry(child, &event->child_list, child_list) {
  2704. perf_event_modify_copy_attr(&child->attr, attr);
  2705. err = func(child, attr);
  2706. if (err)
  2707. goto out;
  2708. }
  2709. out:
  2710. mutex_unlock(&event->child_mutex);
  2711. return err;
  2712. }
  2713. static void ctx_sched_out(struct perf_event_context *ctx,
  2714. struct perf_cpu_context *cpuctx,
  2715. enum event_type_t event_type)
  2716. {
  2717. struct perf_event *event, *tmp;
  2718. int is_active = ctx->is_active;
  2719. lockdep_assert_held(&ctx->lock);
  2720. if (likely(!ctx->nr_events)) {
  2721. /*
  2722. * See __perf_remove_from_context().
  2723. */
  2724. WARN_ON_ONCE(ctx->is_active);
  2725. if (ctx->task)
  2726. WARN_ON_ONCE(cpuctx->task_ctx);
  2727. return;
  2728. }
  2729. /*
  2730. * Always update time if it was set; not only when it changes.
  2731. * Otherwise we can 'forget' to update time for any but the last
  2732. * context we sched out. For example:
  2733. *
  2734. * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
  2735. * ctx_sched_out(.event_type = EVENT_PINNED)
  2736. *
  2737. * would only update time for the pinned events.
  2738. */
  2739. if (is_active & EVENT_TIME) {
  2740. /* update (and stop) ctx time */
  2741. update_context_time(ctx);
  2742. update_cgrp_time_from_cpuctx(cpuctx, ctx == &cpuctx->ctx);
  2743. /*
  2744. * CPU-release for the below ->is_active store,
  2745. * see __load_acquire() in perf_event_time_now()
  2746. */
  2747. barrier();
  2748. }
  2749. ctx->is_active &= ~event_type;
  2750. if (!(ctx->is_active & EVENT_ALL))
  2751. ctx->is_active = 0;
  2752. if (ctx->task) {
  2753. WARN_ON_ONCE(cpuctx->task_ctx != ctx);
  2754. if (!ctx->is_active)
  2755. cpuctx->task_ctx = NULL;
  2756. }
  2757. is_active ^= ctx->is_active; /* changed bits */
  2758. if (!ctx->nr_active || !(is_active & EVENT_ALL))
  2759. return;
  2760. perf_pmu_disable(ctx->pmu);
  2761. if (is_active & EVENT_PINNED) {
  2762. list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list)
  2763. group_sched_out(event, cpuctx, ctx);
  2764. }
  2765. if (is_active & EVENT_FLEXIBLE) {
  2766. list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list)
  2767. group_sched_out(event, cpuctx, ctx);
  2768. /*
  2769. * Since we cleared EVENT_FLEXIBLE, also clear
  2770. * rotate_necessary, is will be reset by
  2771. * ctx_flexible_sched_in() when needed.
  2772. */
  2773. ctx->rotate_necessary = 0;
  2774. }
  2775. perf_pmu_enable(ctx->pmu);
  2776. }
  2777. /*
  2778. * Test whether two contexts are equivalent, i.e. whether they have both been
  2779. * cloned from the same version of the same context.
  2780. *
  2781. * Equivalence is measured using a generation number in the context that is
  2782. * incremented on each modification to it; see unclone_ctx(), list_add_event()
  2783. * and list_del_event().
  2784. */
  2785. static int context_equiv(struct perf_event_context *ctx1,
  2786. struct perf_event_context *ctx2)
  2787. {
  2788. lockdep_assert_held(&ctx1->lock);
  2789. lockdep_assert_held(&ctx2->lock);
  2790. /* Pinning disables the swap optimization */
  2791. if (ctx1->pin_count || ctx2->pin_count)
  2792. return 0;
  2793. /* If ctx1 is the parent of ctx2 */
  2794. if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
  2795. return 1;
  2796. /* If ctx2 is the parent of ctx1 */
  2797. if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
  2798. return 1;
  2799. /*
  2800. * If ctx1 and ctx2 have the same parent; we flatten the parent
  2801. * hierarchy, see perf_event_init_context().
  2802. */
  2803. if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
  2804. ctx1->parent_gen == ctx2->parent_gen)
  2805. return 1;
  2806. /* Unmatched */
  2807. return 0;
  2808. }
  2809. static void __perf_event_sync_stat(struct perf_event *event,
  2810. struct perf_event *next_event)
  2811. {
  2812. u64 value;
  2813. if (!event->attr.inherit_stat)
  2814. return;
  2815. /*
  2816. * Update the event value, we cannot use perf_event_read()
  2817. * because we're in the middle of a context switch and have IRQs
  2818. * disabled, which upsets smp_call_function_single(), however
  2819. * we know the event must be on the current CPU, therefore we
  2820. * don't need to use it.
  2821. */
  2822. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2823. event->pmu->read(event);
  2824. perf_event_update_time(event);
  2825. /*
  2826. * In order to keep per-task stats reliable we need to flip the event
  2827. * values when we flip the contexts.
  2828. */
  2829. value = local64_read(&next_event->count);
  2830. value = local64_xchg(&event->count, value);
  2831. local64_set(&next_event->count, value);
  2832. swap(event->total_time_enabled, next_event->total_time_enabled);
  2833. swap(event->total_time_running, next_event->total_time_running);
  2834. /*
  2835. * Since we swizzled the values, update the user visible data too.
  2836. */
  2837. perf_event_update_userpage(event);
  2838. perf_event_update_userpage(next_event);
  2839. }
  2840. static void perf_event_sync_stat(struct perf_event_context *ctx,
  2841. struct perf_event_context *next_ctx)
  2842. {
  2843. struct perf_event *event, *next_event;
  2844. if (!ctx->nr_stat)
  2845. return;
  2846. update_context_time(ctx);
  2847. event = list_first_entry(&ctx->event_list,
  2848. struct perf_event, event_entry);
  2849. next_event = list_first_entry(&next_ctx->event_list,
  2850. struct perf_event, event_entry);
  2851. while (&event->event_entry != &ctx->event_list &&
  2852. &next_event->event_entry != &next_ctx->event_list) {
  2853. __perf_event_sync_stat(event, next_event);
  2854. event = list_next_entry(event, event_entry);
  2855. next_event = list_next_entry(next_event, event_entry);
  2856. }
  2857. }
  2858. static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  2859. struct task_struct *next)
  2860. {
  2861. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  2862. struct perf_event_context *next_ctx;
  2863. struct perf_event_context *parent, *next_parent;
  2864. struct perf_cpu_context *cpuctx;
  2865. int do_switch = 1;
  2866. struct pmu *pmu;
  2867. if (likely(!ctx))
  2868. return;
  2869. pmu = ctx->pmu;
  2870. cpuctx = __get_cpu_context(ctx);
  2871. if (!cpuctx->task_ctx)
  2872. return;
  2873. rcu_read_lock();
  2874. next_ctx = next->perf_event_ctxp[ctxn];
  2875. if (!next_ctx)
  2876. goto unlock;
  2877. parent = rcu_dereference(ctx->parent_ctx);
  2878. next_parent = rcu_dereference(next_ctx->parent_ctx);
  2879. /* If neither context have a parent context; they cannot be clones. */
  2880. if (!parent && !next_parent)
  2881. goto unlock;
  2882. if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
  2883. /*
  2884. * Looks like the two contexts are clones, so we might be
  2885. * able to optimize the context switch. We lock both
  2886. * contexts and check that they are clones under the
  2887. * lock (including re-checking that neither has been
  2888. * uncloned in the meantime). It doesn't matter which
  2889. * order we take the locks because no other cpu could
  2890. * be trying to lock both of these tasks.
  2891. */
  2892. raw_spin_lock(&ctx->lock);
  2893. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  2894. if (context_equiv(ctx, next_ctx)) {
  2895. perf_pmu_disable(pmu);
  2896. /* PMIs are disabled; ctx->nr_pending is stable. */
  2897. if (local_read(&ctx->nr_pending) ||
  2898. local_read(&next_ctx->nr_pending)) {
  2899. /*
  2900. * Must not swap out ctx when there's pending
  2901. * events that rely on the ctx->task relation.
  2902. */
  2903. raw_spin_unlock(&next_ctx->lock);
  2904. rcu_read_unlock();
  2905. goto inside_switch;
  2906. }
  2907. WRITE_ONCE(ctx->task, next);
  2908. WRITE_ONCE(next_ctx->task, task);
  2909. if (cpuctx->sched_cb_usage && pmu->sched_task)
  2910. pmu->sched_task(ctx, false);
  2911. /*
  2912. * PMU specific parts of task perf context can require
  2913. * additional synchronization. As an example of such
  2914. * synchronization see implementation details of Intel
  2915. * LBR call stack data profiling;
  2916. */
  2917. if (pmu->swap_task_ctx)
  2918. pmu->swap_task_ctx(ctx, next_ctx);
  2919. else
  2920. swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
  2921. perf_pmu_enable(pmu);
  2922. /*
  2923. * RCU_INIT_POINTER here is safe because we've not
  2924. * modified the ctx and the above modification of
  2925. * ctx->task and ctx->task_ctx_data are immaterial
  2926. * since those values are always verified under
  2927. * ctx->lock which we're now holding.
  2928. */
  2929. RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
  2930. RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
  2931. do_switch = 0;
  2932. perf_event_sync_stat(ctx, next_ctx);
  2933. }
  2934. raw_spin_unlock(&next_ctx->lock);
  2935. raw_spin_unlock(&ctx->lock);
  2936. }
  2937. unlock:
  2938. rcu_read_unlock();
  2939. if (do_switch) {
  2940. raw_spin_lock(&ctx->lock);
  2941. perf_pmu_disable(pmu);
  2942. inside_switch:
  2943. if (cpuctx->sched_cb_usage && pmu->sched_task)
  2944. pmu->sched_task(ctx, false);
  2945. task_ctx_sched_out(cpuctx, ctx, EVENT_ALL);
  2946. perf_pmu_enable(pmu);
  2947. raw_spin_unlock(&ctx->lock);
  2948. }
  2949. }
  2950. static DEFINE_PER_CPU(struct list_head, sched_cb_list);
  2951. void perf_sched_cb_dec(struct pmu *pmu)
  2952. {
  2953. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  2954. this_cpu_dec(perf_sched_cb_usages);
  2955. if (!--cpuctx->sched_cb_usage)
  2956. list_del(&cpuctx->sched_cb_entry);
  2957. }
  2958. void perf_sched_cb_inc(struct pmu *pmu)
  2959. {
  2960. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  2961. if (!cpuctx->sched_cb_usage++)
  2962. list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
  2963. this_cpu_inc(perf_sched_cb_usages);
  2964. }
  2965. /*
  2966. * This function provides the context switch callback to the lower code
  2967. * layer. It is invoked ONLY when the context switch callback is enabled.
  2968. *
  2969. * This callback is relevant even to per-cpu events; for example multi event
  2970. * PEBS requires this to provide PID/TID information. This requires we flush
  2971. * all queued PEBS records before we context switch to a new task.
  2972. */
  2973. static void __perf_pmu_sched_task(struct perf_cpu_context *cpuctx, bool sched_in)
  2974. {
  2975. struct pmu *pmu;
  2976. pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */
  2977. if (WARN_ON_ONCE(!pmu->sched_task))
  2978. return;
  2979. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2980. perf_pmu_disable(pmu);
  2981. pmu->sched_task(cpuctx->task_ctx, sched_in);
  2982. perf_pmu_enable(pmu);
  2983. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2984. }
  2985. static void perf_pmu_sched_task(struct task_struct *prev,
  2986. struct task_struct *next,
  2987. bool sched_in)
  2988. {
  2989. struct perf_cpu_context *cpuctx;
  2990. if (prev == next)
  2991. return;
  2992. list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
  2993. /* will be handled in perf_event_context_sched_in/out */
  2994. if (cpuctx->task_ctx)
  2995. continue;
  2996. __perf_pmu_sched_task(cpuctx, sched_in);
  2997. }
  2998. }
  2999. static void perf_event_switch(struct task_struct *task,
  3000. struct task_struct *next_prev, bool sched_in);
  3001. #define for_each_task_context_nr(ctxn) \
  3002. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  3003. /*
  3004. * Called from scheduler to remove the events of the current task,
  3005. * with interrupts disabled.
  3006. *
  3007. * We stop each event and update the event value in event->count.
  3008. *
  3009. * This does not protect us against NMI, but disable()
  3010. * sets the disabled bit in the control field of event _before_
  3011. * accessing the event control register. If a NMI hits, then it will
  3012. * not restart the event.
  3013. */
  3014. void __perf_event_task_sched_out(struct task_struct *task,
  3015. struct task_struct *next)
  3016. {
  3017. int ctxn;
  3018. if (__this_cpu_read(perf_sched_cb_usages))
  3019. perf_pmu_sched_task(task, next, false);
  3020. if (atomic_read(&nr_switch_events))
  3021. perf_event_switch(task, next, false);
  3022. for_each_task_context_nr(ctxn)
  3023. perf_event_context_sched_out(task, ctxn, next);
  3024. /*
  3025. * if cgroup events exist on this CPU, then we need
  3026. * to check if we have to switch out PMU state.
  3027. * cgroup event are system-wide mode only
  3028. */
  3029. if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
  3030. perf_cgroup_switch(next);
  3031. }
  3032. /*
  3033. * Called with IRQs disabled
  3034. */
  3035. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  3036. enum event_type_t event_type)
  3037. {
  3038. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  3039. }
  3040. static bool perf_less_group_idx(const void *l, const void *r)
  3041. {
  3042. const struct perf_event *le = *(const struct perf_event **)l;
  3043. const struct perf_event *re = *(const struct perf_event **)r;
  3044. return le->group_index < re->group_index;
  3045. }
  3046. static void swap_ptr(void *l, void *r)
  3047. {
  3048. void **lp = l, **rp = r;
  3049. swap(*lp, *rp);
  3050. }
  3051. static const struct min_heap_callbacks perf_min_heap = {
  3052. .elem_size = sizeof(struct perf_event *),
  3053. .less = perf_less_group_idx,
  3054. .swp = swap_ptr,
  3055. };
  3056. static void __heap_add(struct min_heap *heap, struct perf_event *event)
  3057. {
  3058. struct perf_event **itrs = heap->data;
  3059. if (event) {
  3060. itrs[heap->nr] = event;
  3061. heap->nr++;
  3062. }
  3063. }
  3064. static noinline int visit_groups_merge(struct perf_cpu_context *cpuctx,
  3065. struct perf_event_groups *groups, int cpu,
  3066. int (*func)(struct perf_event *, void *),
  3067. void *data)
  3068. {
  3069. #ifdef CONFIG_CGROUP_PERF
  3070. struct cgroup_subsys_state *css = NULL;
  3071. #endif
  3072. /* Space for per CPU and/or any CPU event iterators. */
  3073. struct perf_event *itrs[2];
  3074. struct min_heap event_heap;
  3075. struct perf_event **evt;
  3076. int ret;
  3077. if (cpuctx) {
  3078. event_heap = (struct min_heap){
  3079. .data = cpuctx->heap,
  3080. .nr = 0,
  3081. .size = cpuctx->heap_size,
  3082. };
  3083. lockdep_assert_held(&cpuctx->ctx.lock);
  3084. #ifdef CONFIG_CGROUP_PERF
  3085. if (cpuctx->cgrp)
  3086. css = &cpuctx->cgrp->css;
  3087. #endif
  3088. } else {
  3089. event_heap = (struct min_heap){
  3090. .data = itrs,
  3091. .nr = 0,
  3092. .size = ARRAY_SIZE(itrs),
  3093. };
  3094. /* Events not within a CPU context may be on any CPU. */
  3095. __heap_add(&event_heap, perf_event_groups_first(groups, -1, NULL));
  3096. }
  3097. evt = event_heap.data;
  3098. __heap_add(&event_heap, perf_event_groups_first(groups, cpu, NULL));
  3099. #ifdef CONFIG_CGROUP_PERF
  3100. for (; css; css = css->parent)
  3101. __heap_add(&event_heap, perf_event_groups_first(groups, cpu, css->cgroup));
  3102. #endif
  3103. min_heapify_all(&event_heap, &perf_min_heap);
  3104. while (event_heap.nr) {
  3105. ret = func(*evt, data);
  3106. if (ret)
  3107. return ret;
  3108. *evt = perf_event_groups_next(*evt);
  3109. if (*evt)
  3110. min_heapify(&event_heap, 0, &perf_min_heap);
  3111. else
  3112. min_heap_pop(&event_heap, &perf_min_heap);
  3113. }
  3114. return 0;
  3115. }
  3116. /*
  3117. * Because the userpage is strictly per-event (there is no concept of context,
  3118. * so there cannot be a context indirection), every userpage must be updated
  3119. * when context time starts :-(
  3120. *
  3121. * IOW, we must not miss EVENT_TIME edges.
  3122. */
  3123. static inline bool event_update_userpage(struct perf_event *event)
  3124. {
  3125. if (likely(!atomic_read(&event->mmap_count)))
  3126. return false;
  3127. perf_event_update_time(event);
  3128. perf_event_update_userpage(event);
  3129. return true;
  3130. }
  3131. static inline void group_update_userpage(struct perf_event *group_event)
  3132. {
  3133. struct perf_event *event;
  3134. if (!event_update_userpage(group_event))
  3135. return;
  3136. for_each_sibling_event(event, group_event)
  3137. event_update_userpage(event);
  3138. }
  3139. static int merge_sched_in(struct perf_event *event, void *data)
  3140. {
  3141. struct perf_event_context *ctx = event->ctx;
  3142. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  3143. int *can_add_hw = data;
  3144. if (event->state <= PERF_EVENT_STATE_OFF)
  3145. return 0;
  3146. if (!event_filter_match(event))
  3147. return 0;
  3148. if (group_can_go_on(event, cpuctx, *can_add_hw)) {
  3149. if (!group_sched_in(event, cpuctx, ctx))
  3150. list_add_tail(&event->active_list, get_event_list(event));
  3151. }
  3152. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  3153. *can_add_hw = 0;
  3154. if (event->attr.pinned) {
  3155. perf_cgroup_event_disable(event, ctx);
  3156. perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
  3157. } else {
  3158. ctx->rotate_necessary = 1;
  3159. perf_mux_hrtimer_restart(cpuctx);
  3160. group_update_userpage(event);
  3161. }
  3162. }
  3163. return 0;
  3164. }
  3165. static void
  3166. ctx_pinned_sched_in(struct perf_event_context *ctx,
  3167. struct perf_cpu_context *cpuctx)
  3168. {
  3169. int can_add_hw = 1;
  3170. if (ctx != &cpuctx->ctx)
  3171. cpuctx = NULL;
  3172. visit_groups_merge(cpuctx, &ctx->pinned_groups,
  3173. smp_processor_id(),
  3174. merge_sched_in, &can_add_hw);
  3175. }
  3176. static void
  3177. ctx_flexible_sched_in(struct perf_event_context *ctx,
  3178. struct perf_cpu_context *cpuctx)
  3179. {
  3180. int can_add_hw = 1;
  3181. if (ctx != &cpuctx->ctx)
  3182. cpuctx = NULL;
  3183. visit_groups_merge(cpuctx, &ctx->flexible_groups,
  3184. smp_processor_id(),
  3185. merge_sched_in, &can_add_hw);
  3186. }
  3187. static void
  3188. ctx_sched_in(struct perf_event_context *ctx,
  3189. struct perf_cpu_context *cpuctx,
  3190. enum event_type_t event_type)
  3191. {
  3192. int is_active = ctx->is_active;
  3193. lockdep_assert_held(&ctx->lock);
  3194. if (likely(!ctx->nr_events))
  3195. return;
  3196. if (!(is_active & EVENT_TIME)) {
  3197. /* start ctx time */
  3198. __update_context_time(ctx, false);
  3199. perf_cgroup_set_timestamp(cpuctx);
  3200. /*
  3201. * CPU-release for the below ->is_active store,
  3202. * see __load_acquire() in perf_event_time_now()
  3203. */
  3204. barrier();
  3205. }
  3206. ctx->is_active |= (event_type | EVENT_TIME);
  3207. if (ctx->task) {
  3208. if (!is_active)
  3209. cpuctx->task_ctx = ctx;
  3210. else
  3211. WARN_ON_ONCE(cpuctx->task_ctx != ctx);
  3212. }
  3213. is_active ^= ctx->is_active; /* changed bits */
  3214. /*
  3215. * First go through the list and put on any pinned groups
  3216. * in order to give them the best chance of going on.
  3217. */
  3218. if (is_active & EVENT_PINNED)
  3219. ctx_pinned_sched_in(ctx, cpuctx);
  3220. /* Then walk through the lower prio flexible groups */
  3221. if (is_active & EVENT_FLEXIBLE)
  3222. ctx_flexible_sched_in(ctx, cpuctx);
  3223. }
  3224. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  3225. enum event_type_t event_type)
  3226. {
  3227. struct perf_event_context *ctx = &cpuctx->ctx;
  3228. ctx_sched_in(ctx, cpuctx, event_type);
  3229. }
  3230. static void perf_event_context_sched_in(struct perf_event_context *ctx,
  3231. struct task_struct *task)
  3232. {
  3233. struct perf_cpu_context *cpuctx;
  3234. struct pmu *pmu;
  3235. cpuctx = __get_cpu_context(ctx);
  3236. /*
  3237. * HACK: for HETEROGENEOUS the task context might have switched to a
  3238. * different PMU, force (re)set the context,
  3239. */
  3240. pmu = ctx->pmu = cpuctx->ctx.pmu;
  3241. if (cpuctx->task_ctx == ctx) {
  3242. if (cpuctx->sched_cb_usage)
  3243. __perf_pmu_sched_task(cpuctx, true);
  3244. return;
  3245. }
  3246. perf_ctx_lock(cpuctx, ctx);
  3247. /*
  3248. * We must check ctx->nr_events while holding ctx->lock, such
  3249. * that we serialize against perf_install_in_context().
  3250. */
  3251. if (!ctx->nr_events)
  3252. goto unlock;
  3253. perf_pmu_disable(pmu);
  3254. /*
  3255. * We want to keep the following priority order:
  3256. * cpu pinned (that don't need to move), task pinned,
  3257. * cpu flexible, task flexible.
  3258. *
  3259. * However, if task's ctx is not carrying any pinned
  3260. * events, no need to flip the cpuctx's events around.
  3261. */
  3262. if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree))
  3263. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  3264. perf_event_sched_in(cpuctx, ctx);
  3265. if (cpuctx->sched_cb_usage && pmu->sched_task)
  3266. pmu->sched_task(cpuctx->task_ctx, true);
  3267. perf_pmu_enable(pmu);
  3268. unlock:
  3269. perf_ctx_unlock(cpuctx, ctx);
  3270. }
  3271. /*
  3272. * Called from scheduler to add the events of the current task
  3273. * with interrupts disabled.
  3274. *
  3275. * We restore the event value and then enable it.
  3276. *
  3277. * This does not protect us against NMI, but enable()
  3278. * sets the enabled bit in the control field of event _before_
  3279. * accessing the event control register. If a NMI hits, then it will
  3280. * keep the event running.
  3281. */
  3282. void __perf_event_task_sched_in(struct task_struct *prev,
  3283. struct task_struct *task)
  3284. {
  3285. struct perf_event_context *ctx;
  3286. int ctxn;
  3287. for_each_task_context_nr(ctxn) {
  3288. ctx = task->perf_event_ctxp[ctxn];
  3289. if (likely(!ctx))
  3290. continue;
  3291. perf_event_context_sched_in(ctx, task);
  3292. }
  3293. if (atomic_read(&nr_switch_events))
  3294. perf_event_switch(task, prev, true);
  3295. if (__this_cpu_read(perf_sched_cb_usages))
  3296. perf_pmu_sched_task(prev, task, true);
  3297. }
  3298. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  3299. {
  3300. u64 frequency = event->attr.sample_freq;
  3301. u64 sec = NSEC_PER_SEC;
  3302. u64 divisor, dividend;
  3303. int count_fls, nsec_fls, frequency_fls, sec_fls;
  3304. count_fls = fls64(count);
  3305. nsec_fls = fls64(nsec);
  3306. frequency_fls = fls64(frequency);
  3307. sec_fls = 30;
  3308. /*
  3309. * We got @count in @nsec, with a target of sample_freq HZ
  3310. * the target period becomes:
  3311. *
  3312. * @count * 10^9
  3313. * period = -------------------
  3314. * @nsec * sample_freq
  3315. *
  3316. */
  3317. /*
  3318. * Reduce accuracy by one bit such that @a and @b converge
  3319. * to a similar magnitude.
  3320. */
  3321. #define REDUCE_FLS(a, b) \
  3322. do { \
  3323. if (a##_fls > b##_fls) { \
  3324. a >>= 1; \
  3325. a##_fls--; \
  3326. } else { \
  3327. b >>= 1; \
  3328. b##_fls--; \
  3329. } \
  3330. } while (0)
  3331. /*
  3332. * Reduce accuracy until either term fits in a u64, then proceed with
  3333. * the other, so that finally we can do a u64/u64 division.
  3334. */
  3335. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  3336. REDUCE_FLS(nsec, frequency);
  3337. REDUCE_FLS(sec, count);
  3338. }
  3339. if (count_fls + sec_fls > 64) {
  3340. divisor = nsec * frequency;
  3341. while (count_fls + sec_fls > 64) {
  3342. REDUCE_FLS(count, sec);
  3343. divisor >>= 1;
  3344. }
  3345. dividend = count * sec;
  3346. } else {
  3347. dividend = count * sec;
  3348. while (nsec_fls + frequency_fls > 64) {
  3349. REDUCE_FLS(nsec, frequency);
  3350. dividend >>= 1;
  3351. }
  3352. divisor = nsec * frequency;
  3353. }
  3354. if (!divisor)
  3355. return dividend;
  3356. return div64_u64(dividend, divisor);
  3357. }
  3358. static DEFINE_PER_CPU(int, perf_throttled_count);
  3359. static DEFINE_PER_CPU(u64, perf_throttled_seq);
  3360. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
  3361. {
  3362. struct hw_perf_event *hwc = &event->hw;
  3363. s64 period, sample_period;
  3364. s64 delta;
  3365. period = perf_calculate_period(event, nsec, count);
  3366. delta = (s64)(period - hwc->sample_period);
  3367. delta = (delta + 7) / 8; /* low pass filter */
  3368. sample_period = hwc->sample_period + delta;
  3369. if (!sample_period)
  3370. sample_period = 1;
  3371. hwc->sample_period = sample_period;
  3372. if (local64_read(&hwc->period_left) > 8*sample_period) {
  3373. if (disable)
  3374. event->pmu->stop(event, PERF_EF_UPDATE);
  3375. local64_set(&hwc->period_left, 0);
  3376. if (disable)
  3377. event->pmu->start(event, PERF_EF_RELOAD);
  3378. }
  3379. }
  3380. /*
  3381. * combine freq adjustment with unthrottling to avoid two passes over the
  3382. * events. At the same time, make sure, having freq events does not change
  3383. * the rate of unthrottling as that would introduce bias.
  3384. */
  3385. static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
  3386. int needs_unthr)
  3387. {
  3388. struct perf_event *event;
  3389. struct hw_perf_event *hwc;
  3390. u64 now, period = TICK_NSEC;
  3391. s64 delta;
  3392. /*
  3393. * only need to iterate over all events iff:
  3394. * - context have events in frequency mode (needs freq adjust)
  3395. * - there are events to unthrottle on this cpu
  3396. */
  3397. if (!(ctx->nr_freq || needs_unthr))
  3398. return;
  3399. raw_spin_lock(&ctx->lock);
  3400. perf_pmu_disable(ctx->pmu);
  3401. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3402. if (event->state != PERF_EVENT_STATE_ACTIVE)
  3403. continue;
  3404. if (!event_filter_match(event))
  3405. continue;
  3406. perf_pmu_disable(event->pmu);
  3407. hwc = &event->hw;
  3408. if (hwc->interrupts == MAX_INTERRUPTS) {
  3409. hwc->interrupts = 0;
  3410. perf_log_throttle(event, 1);
  3411. event->pmu->start(event, 0);
  3412. }
  3413. if (!event->attr.freq || !event->attr.sample_freq)
  3414. goto next;
  3415. /*
  3416. * stop the event and update event->count
  3417. */
  3418. event->pmu->stop(event, PERF_EF_UPDATE);
  3419. now = local64_read(&event->count);
  3420. delta = now - hwc->freq_count_stamp;
  3421. hwc->freq_count_stamp = now;
  3422. /*
  3423. * restart the event
  3424. * reload only if value has changed
  3425. * we have stopped the event so tell that
  3426. * to perf_adjust_period() to avoid stopping it
  3427. * twice.
  3428. */
  3429. if (delta > 0)
  3430. perf_adjust_period(event, period, delta, false);
  3431. event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
  3432. next:
  3433. perf_pmu_enable(event->pmu);
  3434. }
  3435. perf_pmu_enable(ctx->pmu);
  3436. raw_spin_unlock(&ctx->lock);
  3437. }
  3438. /*
  3439. * Move @event to the tail of the @ctx's elegible events.
  3440. */
  3441. static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event)
  3442. {
  3443. /*
  3444. * Rotate the first entry last of non-pinned groups. Rotation might be
  3445. * disabled by the inheritance code.
  3446. */
  3447. if (ctx->rotate_disable)
  3448. return;
  3449. perf_event_groups_delete(&ctx->flexible_groups, event);
  3450. perf_event_groups_insert(&ctx->flexible_groups, event);
  3451. }
  3452. /* pick an event from the flexible_groups to rotate */
  3453. static inline struct perf_event *
  3454. ctx_event_to_rotate(struct perf_event_context *ctx)
  3455. {
  3456. struct perf_event *event;
  3457. /* pick the first active flexible event */
  3458. event = list_first_entry_or_null(&ctx->flexible_active,
  3459. struct perf_event, active_list);
  3460. /* if no active flexible event, pick the first event */
  3461. if (!event) {
  3462. event = rb_entry_safe(rb_first(&ctx->flexible_groups.tree),
  3463. typeof(*event), group_node);
  3464. }
  3465. /*
  3466. * Unconditionally clear rotate_necessary; if ctx_flexible_sched_in()
  3467. * finds there are unschedulable events, it will set it again.
  3468. */
  3469. ctx->rotate_necessary = 0;
  3470. return event;
  3471. }
  3472. static bool perf_rotate_context(struct perf_cpu_context *cpuctx)
  3473. {
  3474. struct perf_event *cpu_event = NULL, *task_event = NULL;
  3475. struct perf_event_context *task_ctx = NULL;
  3476. int cpu_rotate, task_rotate;
  3477. /*
  3478. * Since we run this from IRQ context, nobody can install new
  3479. * events, thus the event count values are stable.
  3480. */
  3481. cpu_rotate = cpuctx->ctx.rotate_necessary;
  3482. task_ctx = cpuctx->task_ctx;
  3483. task_rotate = task_ctx ? task_ctx->rotate_necessary : 0;
  3484. if (!(cpu_rotate || task_rotate))
  3485. return false;
  3486. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  3487. perf_pmu_disable(cpuctx->ctx.pmu);
  3488. if (task_rotate)
  3489. task_event = ctx_event_to_rotate(task_ctx);
  3490. if (cpu_rotate)
  3491. cpu_event = ctx_event_to_rotate(&cpuctx->ctx);
  3492. /*
  3493. * As per the order given at ctx_resched() first 'pop' task flexible
  3494. * and then, if needed CPU flexible.
  3495. */
  3496. if (task_event || (task_ctx && cpu_event))
  3497. ctx_sched_out(task_ctx, cpuctx, EVENT_FLEXIBLE);
  3498. if (cpu_event)
  3499. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  3500. if (task_event)
  3501. rotate_ctx(task_ctx, task_event);
  3502. if (cpu_event)
  3503. rotate_ctx(&cpuctx->ctx, cpu_event);
  3504. perf_event_sched_in(cpuctx, task_ctx);
  3505. perf_pmu_enable(cpuctx->ctx.pmu);
  3506. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  3507. return true;
  3508. }
  3509. void perf_event_task_tick(void)
  3510. {
  3511. struct list_head *head = this_cpu_ptr(&active_ctx_list);
  3512. struct perf_event_context *ctx, *tmp;
  3513. int throttled;
  3514. lockdep_assert_irqs_disabled();
  3515. __this_cpu_inc(perf_throttled_seq);
  3516. throttled = __this_cpu_xchg(perf_throttled_count, 0);
  3517. tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
  3518. list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
  3519. perf_adjust_freq_unthr_context(ctx, throttled);
  3520. }
  3521. static int event_enable_on_exec(struct perf_event *event,
  3522. struct perf_event_context *ctx)
  3523. {
  3524. if (!event->attr.enable_on_exec)
  3525. return 0;
  3526. event->attr.enable_on_exec = 0;
  3527. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  3528. return 0;
  3529. perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
  3530. return 1;
  3531. }
  3532. /*
  3533. * Enable all of a task's events that have been marked enable-on-exec.
  3534. * This expects task == current.
  3535. */
  3536. static void perf_event_enable_on_exec(int ctxn)
  3537. {
  3538. struct perf_event_context *ctx, *clone_ctx = NULL;
  3539. enum event_type_t event_type = 0;
  3540. struct perf_cpu_context *cpuctx;
  3541. struct perf_event *event;
  3542. unsigned long flags;
  3543. int enabled = 0;
  3544. local_irq_save(flags);
  3545. ctx = current->perf_event_ctxp[ctxn];
  3546. if (!ctx || !ctx->nr_events)
  3547. goto out;
  3548. cpuctx = __get_cpu_context(ctx);
  3549. perf_ctx_lock(cpuctx, ctx);
  3550. ctx_sched_out(ctx, cpuctx, EVENT_TIME);
  3551. list_for_each_entry(event, &ctx->event_list, event_entry) {
  3552. enabled |= event_enable_on_exec(event, ctx);
  3553. event_type |= get_event_type(event);
  3554. }
  3555. /*
  3556. * Unclone and reschedule this context if we enabled any event.
  3557. */
  3558. if (enabled) {
  3559. clone_ctx = unclone_ctx(ctx);
  3560. ctx_resched(cpuctx, ctx, event_type);
  3561. } else {
  3562. ctx_sched_in(ctx, cpuctx, EVENT_TIME);
  3563. }
  3564. perf_ctx_unlock(cpuctx, ctx);
  3565. out:
  3566. local_irq_restore(flags);
  3567. if (clone_ctx)
  3568. put_ctx(clone_ctx);
  3569. }
  3570. static void perf_remove_from_owner(struct perf_event *event);
  3571. static void perf_event_exit_event(struct perf_event *event,
  3572. struct perf_event_context *ctx);
  3573. /*
  3574. * Removes all events from the current task that have been marked
  3575. * remove-on-exec, and feeds their values back to parent events.
  3576. */
  3577. static void perf_event_remove_on_exec(int ctxn)
  3578. {
  3579. struct perf_event_context *ctx, *clone_ctx = NULL;
  3580. struct perf_event *event, *next;
  3581. unsigned long flags;
  3582. bool modified = false;
  3583. ctx = perf_pin_task_context(current, ctxn);
  3584. if (!ctx)
  3585. return;
  3586. mutex_lock(&ctx->mutex);
  3587. if (WARN_ON_ONCE(ctx->task != current))
  3588. goto unlock;
  3589. list_for_each_entry_safe(event, next, &ctx->event_list, event_entry) {
  3590. if (!event->attr.remove_on_exec)
  3591. continue;
  3592. if (!is_kernel_event(event))
  3593. perf_remove_from_owner(event);
  3594. modified = true;
  3595. perf_event_exit_event(event, ctx);
  3596. }
  3597. raw_spin_lock_irqsave(&ctx->lock, flags);
  3598. if (modified)
  3599. clone_ctx = unclone_ctx(ctx);
  3600. --ctx->pin_count;
  3601. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  3602. unlock:
  3603. mutex_unlock(&ctx->mutex);
  3604. put_ctx(ctx);
  3605. if (clone_ctx)
  3606. put_ctx(clone_ctx);
  3607. }
  3608. struct perf_read_data {
  3609. struct perf_event *event;
  3610. bool group;
  3611. int ret;
  3612. };
  3613. static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
  3614. {
  3615. u16 local_pkg, event_pkg;
  3616. if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
  3617. int local_cpu = smp_processor_id();
  3618. event_pkg = topology_physical_package_id(event_cpu);
  3619. local_pkg = topology_physical_package_id(local_cpu);
  3620. if (event_pkg == local_pkg)
  3621. return local_cpu;
  3622. }
  3623. return event_cpu;
  3624. }
  3625. /*
  3626. * Cross CPU call to read the hardware event
  3627. */
  3628. static void __perf_event_read(void *info)
  3629. {
  3630. struct perf_read_data *data = info;
  3631. struct perf_event *sub, *event = data->event;
  3632. struct perf_event_context *ctx = event->ctx;
  3633. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  3634. struct pmu *pmu = event->pmu;
  3635. /*
  3636. * If this is a task context, we need to check whether it is
  3637. * the current task context of this cpu. If not it has been
  3638. * scheduled out before the smp call arrived. In that case
  3639. * event->count would have been updated to a recent sample
  3640. * when the event was scheduled out.
  3641. */
  3642. if (ctx->task && cpuctx->task_ctx != ctx)
  3643. return;
  3644. raw_spin_lock(&ctx->lock);
  3645. if (ctx->is_active & EVENT_TIME) {
  3646. update_context_time(ctx);
  3647. update_cgrp_time_from_event(event);
  3648. }
  3649. perf_event_update_time(event);
  3650. if (data->group)
  3651. perf_event_update_sibling_time(event);
  3652. if (event->state != PERF_EVENT_STATE_ACTIVE)
  3653. goto unlock;
  3654. if (!data->group) {
  3655. pmu->read(event);
  3656. data->ret = 0;
  3657. goto unlock;
  3658. }
  3659. pmu->start_txn(pmu, PERF_PMU_TXN_READ);
  3660. pmu->read(event);
  3661. for_each_sibling_event(sub, event) {
  3662. if (sub->state == PERF_EVENT_STATE_ACTIVE) {
  3663. /*
  3664. * Use sibling's PMU rather than @event's since
  3665. * sibling could be on different (eg: software) PMU.
  3666. */
  3667. sub->pmu->read(sub);
  3668. }
  3669. }
  3670. data->ret = pmu->commit_txn(pmu);
  3671. unlock:
  3672. raw_spin_unlock(&ctx->lock);
  3673. }
  3674. static inline u64 perf_event_count(struct perf_event *event)
  3675. {
  3676. return local64_read(&event->count) + atomic64_read(&event->child_count);
  3677. }
  3678. static void calc_timer_values(struct perf_event *event,
  3679. u64 *now,
  3680. u64 *enabled,
  3681. u64 *running)
  3682. {
  3683. u64 ctx_time;
  3684. *now = perf_clock();
  3685. ctx_time = perf_event_time_now(event, *now);
  3686. __perf_update_times(event, ctx_time, enabled, running);
  3687. }
  3688. /*
  3689. * NMI-safe method to read a local event, that is an event that
  3690. * is:
  3691. * - either for the current task, or for this CPU
  3692. * - does not have inherit set, for inherited task events
  3693. * will not be local and we cannot read them atomically
  3694. * - must not have a pmu::count method
  3695. */
  3696. int perf_event_read_local(struct perf_event *event, u64 *value,
  3697. u64 *enabled, u64 *running)
  3698. {
  3699. unsigned long flags;
  3700. int ret = 0;
  3701. /*
  3702. * Disabling interrupts avoids all counter scheduling (context
  3703. * switches, timer based rotation and IPIs).
  3704. */
  3705. local_irq_save(flags);
  3706. /*
  3707. * It must not be an event with inherit set, we cannot read
  3708. * all child counters from atomic context.
  3709. */
  3710. if (event->attr.inherit) {
  3711. ret = -EOPNOTSUPP;
  3712. goto out;
  3713. }
  3714. /* If this is a per-task event, it must be for current */
  3715. if ((event->attach_state & PERF_ATTACH_TASK) &&
  3716. event->hw.target != current) {
  3717. ret = -EINVAL;
  3718. goto out;
  3719. }
  3720. /* If this is a per-CPU event, it must be for this CPU */
  3721. if (!(event->attach_state & PERF_ATTACH_TASK) &&
  3722. event->cpu != smp_processor_id()) {
  3723. ret = -EINVAL;
  3724. goto out;
  3725. }
  3726. /* If this is a pinned event it must be running on this CPU */
  3727. if (event->attr.pinned && event->oncpu != smp_processor_id()) {
  3728. ret = -EBUSY;
  3729. goto out;
  3730. }
  3731. /*
  3732. * If the event is currently on this CPU, its either a per-task event,
  3733. * or local to this CPU. Furthermore it means its ACTIVE (otherwise
  3734. * oncpu == -1).
  3735. */
  3736. if (event->oncpu == smp_processor_id())
  3737. event->pmu->read(event);
  3738. *value = local64_read(&event->count);
  3739. if (enabled || running) {
  3740. u64 __enabled, __running, __now;
  3741. calc_timer_values(event, &__now, &__enabled, &__running);
  3742. if (enabled)
  3743. *enabled = __enabled;
  3744. if (running)
  3745. *running = __running;
  3746. }
  3747. out:
  3748. local_irq_restore(flags);
  3749. return ret;
  3750. }
  3751. EXPORT_SYMBOL_GPL(perf_event_read_local);
  3752. static int perf_event_read(struct perf_event *event, bool group)
  3753. {
  3754. enum perf_event_state state = READ_ONCE(event->state);
  3755. int event_cpu, ret = 0;
  3756. /*
  3757. * If event is enabled and currently active on a CPU, update the
  3758. * value in the event structure:
  3759. */
  3760. again:
  3761. if (state == PERF_EVENT_STATE_ACTIVE) {
  3762. struct perf_read_data data;
  3763. /*
  3764. * Orders the ->state and ->oncpu loads such that if we see
  3765. * ACTIVE we must also see the right ->oncpu.
  3766. *
  3767. * Matches the smp_wmb() from event_sched_in().
  3768. */
  3769. smp_rmb();
  3770. event_cpu = READ_ONCE(event->oncpu);
  3771. if ((unsigned)event_cpu >= nr_cpu_ids)
  3772. return 0;
  3773. data = (struct perf_read_data){
  3774. .event = event,
  3775. .group = group,
  3776. .ret = 0,
  3777. };
  3778. preempt_disable();
  3779. event_cpu = __perf_event_read_cpu(event, event_cpu);
  3780. /*
  3781. * Purposely ignore the smp_call_function_single() return
  3782. * value.
  3783. *
  3784. * If event_cpu isn't a valid CPU it means the event got
  3785. * scheduled out and that will have updated the event count.
  3786. *
  3787. * Therefore, either way, we'll have an up-to-date event count
  3788. * after this.
  3789. */
  3790. (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1);
  3791. preempt_enable();
  3792. ret = data.ret;
  3793. } else if (state == PERF_EVENT_STATE_INACTIVE) {
  3794. struct perf_event_context *ctx = event->ctx;
  3795. unsigned long flags;
  3796. raw_spin_lock_irqsave(&ctx->lock, flags);
  3797. state = event->state;
  3798. if (state != PERF_EVENT_STATE_INACTIVE) {
  3799. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  3800. goto again;
  3801. }
  3802. /*
  3803. * May read while context is not active (e.g., thread is
  3804. * blocked), in that case we cannot update context time
  3805. */
  3806. if (ctx->is_active & EVENT_TIME) {
  3807. update_context_time(ctx);
  3808. update_cgrp_time_from_event(event);
  3809. }
  3810. perf_event_update_time(event);
  3811. if (group)
  3812. perf_event_update_sibling_time(event);
  3813. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  3814. }
  3815. return ret;
  3816. }
  3817. /*
  3818. * Initialize the perf_event context in a task_struct:
  3819. */
  3820. static void __perf_event_init_context(struct perf_event_context *ctx)
  3821. {
  3822. raw_spin_lock_init(&ctx->lock);
  3823. mutex_init(&ctx->mutex);
  3824. INIT_LIST_HEAD(&ctx->active_ctx_list);
  3825. perf_event_groups_init(&ctx->pinned_groups);
  3826. perf_event_groups_init(&ctx->flexible_groups);
  3827. INIT_LIST_HEAD(&ctx->event_list);
  3828. INIT_LIST_HEAD(&ctx->pinned_active);
  3829. INIT_LIST_HEAD(&ctx->flexible_active);
  3830. refcount_set(&ctx->refcount, 1);
  3831. }
  3832. static struct perf_event_context *
  3833. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  3834. {
  3835. struct perf_event_context *ctx;
  3836. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  3837. if (!ctx)
  3838. return NULL;
  3839. __perf_event_init_context(ctx);
  3840. if (task)
  3841. ctx->task = get_task_struct(task);
  3842. ctx->pmu = pmu;
  3843. return ctx;
  3844. }
  3845. static struct task_struct *
  3846. find_lively_task_by_vpid(pid_t vpid)
  3847. {
  3848. struct task_struct *task;
  3849. rcu_read_lock();
  3850. if (!vpid)
  3851. task = current;
  3852. else
  3853. task = find_task_by_vpid(vpid);
  3854. if (task)
  3855. get_task_struct(task);
  3856. rcu_read_unlock();
  3857. if (!task)
  3858. return ERR_PTR(-ESRCH);
  3859. return task;
  3860. }
  3861. /*
  3862. * Returns a matching context with refcount and pincount.
  3863. */
  3864. static struct perf_event_context *
  3865. find_get_context(struct pmu *pmu, struct task_struct *task,
  3866. struct perf_event *event)
  3867. {
  3868. struct perf_event_context *ctx, *clone_ctx = NULL;
  3869. struct perf_cpu_context *cpuctx;
  3870. void *task_ctx_data = NULL;
  3871. unsigned long flags;
  3872. int ctxn, err;
  3873. int cpu = event->cpu;
  3874. if (!task) {
  3875. /* Must be root to operate on a CPU event: */
  3876. err = perf_allow_cpu(&event->attr);
  3877. if (err)
  3878. return ERR_PTR(err);
  3879. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  3880. ctx = &cpuctx->ctx;
  3881. get_ctx(ctx);
  3882. raw_spin_lock_irqsave(&ctx->lock, flags);
  3883. ++ctx->pin_count;
  3884. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  3885. return ctx;
  3886. }
  3887. err = -EINVAL;
  3888. ctxn = pmu->task_ctx_nr;
  3889. if (ctxn < 0)
  3890. goto errout;
  3891. if (event->attach_state & PERF_ATTACH_TASK_DATA) {
  3892. task_ctx_data = alloc_task_ctx_data(pmu);
  3893. if (!task_ctx_data) {
  3894. err = -ENOMEM;
  3895. goto errout;
  3896. }
  3897. }
  3898. retry:
  3899. ctx = perf_lock_task_context(task, ctxn, &flags);
  3900. if (ctx) {
  3901. clone_ctx = unclone_ctx(ctx);
  3902. ++ctx->pin_count;
  3903. if (task_ctx_data && !ctx->task_ctx_data) {
  3904. ctx->task_ctx_data = task_ctx_data;
  3905. task_ctx_data = NULL;
  3906. }
  3907. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  3908. if (clone_ctx)
  3909. put_ctx(clone_ctx);
  3910. } else {
  3911. ctx = alloc_perf_context(pmu, task);
  3912. err = -ENOMEM;
  3913. if (!ctx)
  3914. goto errout;
  3915. if (task_ctx_data) {
  3916. ctx->task_ctx_data = task_ctx_data;
  3917. task_ctx_data = NULL;
  3918. }
  3919. err = 0;
  3920. mutex_lock(&task->perf_event_mutex);
  3921. /*
  3922. * If it has already passed perf_event_exit_task().
  3923. * we must see PF_EXITING, it takes this mutex too.
  3924. */
  3925. if (task->flags & PF_EXITING)
  3926. err = -ESRCH;
  3927. else if (task->perf_event_ctxp[ctxn])
  3928. err = -EAGAIN;
  3929. else {
  3930. get_ctx(ctx);
  3931. ++ctx->pin_count;
  3932. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  3933. }
  3934. mutex_unlock(&task->perf_event_mutex);
  3935. if (unlikely(err)) {
  3936. put_ctx(ctx);
  3937. if (err == -EAGAIN)
  3938. goto retry;
  3939. goto errout;
  3940. }
  3941. }
  3942. free_task_ctx_data(pmu, task_ctx_data);
  3943. return ctx;
  3944. errout:
  3945. free_task_ctx_data(pmu, task_ctx_data);
  3946. return ERR_PTR(err);
  3947. }
  3948. static void perf_event_free_filter(struct perf_event *event);
  3949. static void free_event_rcu(struct rcu_head *head)
  3950. {
  3951. struct perf_event *event;
  3952. event = container_of(head, struct perf_event, rcu_head);
  3953. if (event->ns)
  3954. put_pid_ns(event->ns);
  3955. perf_event_free_filter(event);
  3956. kmem_cache_free(perf_event_cache, event);
  3957. }
  3958. static void ring_buffer_attach(struct perf_event *event,
  3959. struct perf_buffer *rb);
  3960. static void detach_sb_event(struct perf_event *event)
  3961. {
  3962. struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
  3963. raw_spin_lock(&pel->lock);
  3964. list_del_rcu(&event->sb_list);
  3965. raw_spin_unlock(&pel->lock);
  3966. }
  3967. static bool is_sb_event(struct perf_event *event)
  3968. {
  3969. struct perf_event_attr *attr = &event->attr;
  3970. if (event->parent)
  3971. return false;
  3972. if (event->attach_state & PERF_ATTACH_TASK)
  3973. return false;
  3974. if (attr->mmap || attr->mmap_data || attr->mmap2 ||
  3975. attr->comm || attr->comm_exec ||
  3976. attr->task || attr->ksymbol ||
  3977. attr->context_switch || attr->text_poke ||
  3978. attr->bpf_event)
  3979. return true;
  3980. return false;
  3981. }
  3982. static void unaccount_pmu_sb_event(struct perf_event *event)
  3983. {
  3984. if (is_sb_event(event))
  3985. detach_sb_event(event);
  3986. }
  3987. static void unaccount_event_cpu(struct perf_event *event, int cpu)
  3988. {
  3989. if (event->parent)
  3990. return;
  3991. if (is_cgroup_event(event))
  3992. atomic_dec(&per_cpu(perf_cgroup_events, cpu));
  3993. }
  3994. #ifdef CONFIG_NO_HZ_FULL
  3995. static DEFINE_SPINLOCK(nr_freq_lock);
  3996. #endif
  3997. static void unaccount_freq_event_nohz(void)
  3998. {
  3999. #ifdef CONFIG_NO_HZ_FULL
  4000. spin_lock(&nr_freq_lock);
  4001. if (atomic_dec_and_test(&nr_freq_events))
  4002. tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
  4003. spin_unlock(&nr_freq_lock);
  4004. #endif
  4005. }
  4006. static void unaccount_freq_event(void)
  4007. {
  4008. if (tick_nohz_full_enabled())
  4009. unaccount_freq_event_nohz();
  4010. else
  4011. atomic_dec(&nr_freq_events);
  4012. }
  4013. static void unaccount_event(struct perf_event *event)
  4014. {
  4015. bool dec = false;
  4016. if (event->parent)
  4017. return;
  4018. if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB))
  4019. dec = true;
  4020. if (event->attr.mmap || event->attr.mmap_data)
  4021. atomic_dec(&nr_mmap_events);
  4022. if (event->attr.build_id)
  4023. atomic_dec(&nr_build_id_events);
  4024. if (event->attr.comm)
  4025. atomic_dec(&nr_comm_events);
  4026. if (event->attr.namespaces)
  4027. atomic_dec(&nr_namespaces_events);
  4028. if (event->attr.cgroup)
  4029. atomic_dec(&nr_cgroup_events);
  4030. if (event->attr.task)
  4031. atomic_dec(&nr_task_events);
  4032. if (event->attr.freq)
  4033. unaccount_freq_event();
  4034. if (event->attr.context_switch) {
  4035. dec = true;
  4036. atomic_dec(&nr_switch_events);
  4037. }
  4038. if (is_cgroup_event(event))
  4039. dec = true;
  4040. if (has_branch_stack(event))
  4041. dec = true;
  4042. if (event->attr.ksymbol)
  4043. atomic_dec(&nr_ksymbol_events);
  4044. if (event->attr.bpf_event)
  4045. atomic_dec(&nr_bpf_events);
  4046. if (event->attr.text_poke)
  4047. atomic_dec(&nr_text_poke_events);
  4048. if (dec) {
  4049. if (!atomic_add_unless(&perf_sched_count, -1, 1))
  4050. schedule_delayed_work(&perf_sched_work, HZ);
  4051. }
  4052. unaccount_event_cpu(event, event->cpu);
  4053. unaccount_pmu_sb_event(event);
  4054. }
  4055. static void perf_sched_delayed(struct work_struct *work)
  4056. {
  4057. mutex_lock(&perf_sched_mutex);
  4058. if (atomic_dec_and_test(&perf_sched_count))
  4059. static_branch_disable(&perf_sched_events);
  4060. mutex_unlock(&perf_sched_mutex);
  4061. }
  4062. /*
  4063. * The following implement mutual exclusion of events on "exclusive" pmus
  4064. * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
  4065. * at a time, so we disallow creating events that might conflict, namely:
  4066. *
  4067. * 1) cpu-wide events in the presence of per-task events,
  4068. * 2) per-task events in the presence of cpu-wide events,
  4069. * 3) two matching events on the same context.
  4070. *
  4071. * The former two cases are handled in the allocation path (perf_event_alloc(),
  4072. * _free_event()), the latter -- before the first perf_install_in_context().
  4073. */
  4074. static int exclusive_event_init(struct perf_event *event)
  4075. {
  4076. struct pmu *pmu = event->pmu;
  4077. if (!is_exclusive_pmu(pmu))
  4078. return 0;
  4079. /*
  4080. * Prevent co-existence of per-task and cpu-wide events on the
  4081. * same exclusive pmu.
  4082. *
  4083. * Negative pmu::exclusive_cnt means there are cpu-wide
  4084. * events on this "exclusive" pmu, positive means there are
  4085. * per-task events.
  4086. *
  4087. * Since this is called in perf_event_alloc() path, event::ctx
  4088. * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
  4089. * to mean "per-task event", because unlike other attach states it
  4090. * never gets cleared.
  4091. */
  4092. if (event->attach_state & PERF_ATTACH_TASK) {
  4093. if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
  4094. return -EBUSY;
  4095. } else {
  4096. if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
  4097. return -EBUSY;
  4098. }
  4099. return 0;
  4100. }
  4101. static void exclusive_event_destroy(struct perf_event *event)
  4102. {
  4103. struct pmu *pmu = event->pmu;
  4104. if (!is_exclusive_pmu(pmu))
  4105. return;
  4106. /* see comment in exclusive_event_init() */
  4107. if (event->attach_state & PERF_ATTACH_TASK)
  4108. atomic_dec(&pmu->exclusive_cnt);
  4109. else
  4110. atomic_inc(&pmu->exclusive_cnt);
  4111. }
  4112. static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
  4113. {
  4114. if ((e1->pmu == e2->pmu) &&
  4115. (e1->cpu == e2->cpu ||
  4116. e1->cpu == -1 ||
  4117. e2->cpu == -1))
  4118. return true;
  4119. return false;
  4120. }
  4121. static bool exclusive_event_installable(struct perf_event *event,
  4122. struct perf_event_context *ctx)
  4123. {
  4124. struct perf_event *iter_event;
  4125. struct pmu *pmu = event->pmu;
  4126. lockdep_assert_held(&ctx->mutex);
  4127. if (!is_exclusive_pmu(pmu))
  4128. return true;
  4129. list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
  4130. if (exclusive_event_match(iter_event, event))
  4131. return false;
  4132. }
  4133. return true;
  4134. }
  4135. static void perf_addr_filters_splice(struct perf_event *event,
  4136. struct list_head *head);
  4137. static void _free_event(struct perf_event *event)
  4138. {
  4139. irq_work_sync(&event->pending_irq);
  4140. unaccount_event(event);
  4141. security_perf_event_free(event);
  4142. if (event->rb) {
  4143. /*
  4144. * Can happen when we close an event with re-directed output.
  4145. *
  4146. * Since we have a 0 refcount, perf_mmap_close() will skip
  4147. * over us; possibly making our ring_buffer_put() the last.
  4148. */
  4149. mutex_lock(&event->mmap_mutex);
  4150. ring_buffer_attach(event, NULL);
  4151. mutex_unlock(&event->mmap_mutex);
  4152. }
  4153. if (is_cgroup_event(event))
  4154. perf_detach_cgroup(event);
  4155. if (!event->parent) {
  4156. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  4157. put_callchain_buffers();
  4158. }
  4159. perf_event_free_bpf_prog(event);
  4160. perf_addr_filters_splice(event, NULL);
  4161. kfree(event->addr_filter_ranges);
  4162. if (event->destroy)
  4163. event->destroy(event);
  4164. /*
  4165. * Must be after ->destroy(), due to uprobe_perf_close() using
  4166. * hw.target.
  4167. */
  4168. if (event->hw.target)
  4169. put_task_struct(event->hw.target);
  4170. /*
  4171. * perf_event_free_task() relies on put_ctx() being 'last', in particular
  4172. * all task references must be cleaned up.
  4173. */
  4174. if (event->ctx)
  4175. put_ctx(event->ctx);
  4176. exclusive_event_destroy(event);
  4177. module_put(event->pmu->module);
  4178. call_rcu(&event->rcu_head, free_event_rcu);
  4179. }
  4180. /*
  4181. * Used to free events which have a known refcount of 1, such as in error paths
  4182. * where the event isn't exposed yet and inherited events.
  4183. */
  4184. static void free_event(struct perf_event *event)
  4185. {
  4186. if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
  4187. "unexpected event refcount: %ld; ptr=%p\n",
  4188. atomic_long_read(&event->refcount), event)) {
  4189. /* leak to avoid use-after-free */
  4190. return;
  4191. }
  4192. _free_event(event);
  4193. }
  4194. /*
  4195. * Remove user event from the owner task.
  4196. */
  4197. static void perf_remove_from_owner(struct perf_event *event)
  4198. {
  4199. struct task_struct *owner;
  4200. rcu_read_lock();
  4201. /*
  4202. * Matches the smp_store_release() in perf_event_exit_task(). If we
  4203. * observe !owner it means the list deletion is complete and we can
  4204. * indeed free this event, otherwise we need to serialize on
  4205. * owner->perf_event_mutex.
  4206. */
  4207. owner = READ_ONCE(event->owner);
  4208. if (owner) {
  4209. /*
  4210. * Since delayed_put_task_struct() also drops the last
  4211. * task reference we can safely take a new reference
  4212. * while holding the rcu_read_lock().
  4213. */
  4214. get_task_struct(owner);
  4215. }
  4216. rcu_read_unlock();
  4217. if (owner) {
  4218. /*
  4219. * If we're here through perf_event_exit_task() we're already
  4220. * holding ctx->mutex which would be an inversion wrt. the
  4221. * normal lock order.
  4222. *
  4223. * However we can safely take this lock because its the child
  4224. * ctx->mutex.
  4225. */
  4226. mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
  4227. /*
  4228. * We have to re-check the event->owner field, if it is cleared
  4229. * we raced with perf_event_exit_task(), acquiring the mutex
  4230. * ensured they're done, and we can proceed with freeing the
  4231. * event.
  4232. */
  4233. if (event->owner) {
  4234. list_del_init(&event->owner_entry);
  4235. smp_store_release(&event->owner, NULL);
  4236. }
  4237. mutex_unlock(&owner->perf_event_mutex);
  4238. put_task_struct(owner);
  4239. }
  4240. }
  4241. static void put_event(struct perf_event *event)
  4242. {
  4243. if (!atomic_long_dec_and_test(&event->refcount))
  4244. return;
  4245. _free_event(event);
  4246. }
  4247. /*
  4248. * Kill an event dead; while event:refcount will preserve the event
  4249. * object, it will not preserve its functionality. Once the last 'user'
  4250. * gives up the object, we'll destroy the thing.
  4251. */
  4252. int perf_event_release_kernel(struct perf_event *event)
  4253. {
  4254. struct perf_event_context *ctx = event->ctx;
  4255. struct perf_event *child, *tmp;
  4256. LIST_HEAD(free_list);
  4257. /*
  4258. * If we got here through err_file: fput(event_file); we will not have
  4259. * attached to a context yet.
  4260. */
  4261. if (!ctx) {
  4262. WARN_ON_ONCE(event->attach_state &
  4263. (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
  4264. goto no_ctx;
  4265. }
  4266. if (!is_kernel_event(event))
  4267. perf_remove_from_owner(event);
  4268. ctx = perf_event_ctx_lock(event);
  4269. WARN_ON_ONCE(ctx->parent_ctx);
  4270. /*
  4271. * Mark this event as STATE_DEAD, there is no external reference to it
  4272. * anymore.
  4273. *
  4274. * Anybody acquiring event->child_mutex after the below loop _must_
  4275. * also see this, most importantly inherit_event() which will avoid
  4276. * placing more children on the list.
  4277. *
  4278. * Thus this guarantees that we will in fact observe and kill _ALL_
  4279. * child events.
  4280. */
  4281. perf_remove_from_context(event, DETACH_GROUP|DETACH_DEAD);
  4282. perf_event_ctx_unlock(event, ctx);
  4283. again:
  4284. mutex_lock(&event->child_mutex);
  4285. list_for_each_entry(child, &event->child_list, child_list) {
  4286. /*
  4287. * Cannot change, child events are not migrated, see the
  4288. * comment with perf_event_ctx_lock_nested().
  4289. */
  4290. ctx = READ_ONCE(child->ctx);
  4291. /*
  4292. * Since child_mutex nests inside ctx::mutex, we must jump
  4293. * through hoops. We start by grabbing a reference on the ctx.
  4294. *
  4295. * Since the event cannot get freed while we hold the
  4296. * child_mutex, the context must also exist and have a !0
  4297. * reference count.
  4298. */
  4299. get_ctx(ctx);
  4300. /*
  4301. * Now that we have a ctx ref, we can drop child_mutex, and
  4302. * acquire ctx::mutex without fear of it going away. Then we
  4303. * can re-acquire child_mutex.
  4304. */
  4305. mutex_unlock(&event->child_mutex);
  4306. mutex_lock(&ctx->mutex);
  4307. mutex_lock(&event->child_mutex);
  4308. /*
  4309. * Now that we hold ctx::mutex and child_mutex, revalidate our
  4310. * state, if child is still the first entry, it didn't get freed
  4311. * and we can continue doing so.
  4312. */
  4313. tmp = list_first_entry_or_null(&event->child_list,
  4314. struct perf_event, child_list);
  4315. if (tmp == child) {
  4316. perf_remove_from_context(child, DETACH_GROUP);
  4317. list_move(&child->child_list, &free_list);
  4318. /*
  4319. * This matches the refcount bump in inherit_event();
  4320. * this can't be the last reference.
  4321. */
  4322. put_event(event);
  4323. }
  4324. mutex_unlock(&event->child_mutex);
  4325. mutex_unlock(&ctx->mutex);
  4326. put_ctx(ctx);
  4327. goto again;
  4328. }
  4329. mutex_unlock(&event->child_mutex);
  4330. list_for_each_entry_safe(child, tmp, &free_list, child_list) {
  4331. void *var = &child->ctx->refcount;
  4332. list_del(&child->child_list);
  4333. free_event(child);
  4334. /*
  4335. * Wake any perf_event_free_task() waiting for this event to be
  4336. * freed.
  4337. */
  4338. smp_mb(); /* pairs with wait_var_event() */
  4339. wake_up_var(var);
  4340. }
  4341. no_ctx:
  4342. put_event(event); /* Must be the 'last' reference */
  4343. return 0;
  4344. }
  4345. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  4346. /*
  4347. * Called when the last reference to the file is gone.
  4348. */
  4349. static int perf_release(struct inode *inode, struct file *file)
  4350. {
  4351. perf_event_release_kernel(file->private_data);
  4352. return 0;
  4353. }
  4354. static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  4355. {
  4356. struct perf_event *child;
  4357. u64 total = 0;
  4358. *enabled = 0;
  4359. *running = 0;
  4360. mutex_lock(&event->child_mutex);
  4361. (void)perf_event_read(event, false);
  4362. total += perf_event_count(event);
  4363. *enabled += event->total_time_enabled +
  4364. atomic64_read(&event->child_total_time_enabled);
  4365. *running += event->total_time_running +
  4366. atomic64_read(&event->child_total_time_running);
  4367. list_for_each_entry(child, &event->child_list, child_list) {
  4368. (void)perf_event_read(child, false);
  4369. total += perf_event_count(child);
  4370. *enabled += child->total_time_enabled;
  4371. *running += child->total_time_running;
  4372. }
  4373. mutex_unlock(&event->child_mutex);
  4374. return total;
  4375. }
  4376. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  4377. {
  4378. struct perf_event_context *ctx;
  4379. u64 count;
  4380. ctx = perf_event_ctx_lock(event);
  4381. count = __perf_event_read_value(event, enabled, running);
  4382. perf_event_ctx_unlock(event, ctx);
  4383. return count;
  4384. }
  4385. EXPORT_SYMBOL_GPL(perf_event_read_value);
  4386. static int __perf_read_group_add(struct perf_event *leader,
  4387. u64 read_format, u64 *values)
  4388. {
  4389. struct perf_event_context *ctx = leader->ctx;
  4390. struct perf_event *sub, *parent;
  4391. unsigned long flags;
  4392. int n = 1; /* skip @nr */
  4393. int ret;
  4394. ret = perf_event_read(leader, true);
  4395. if (ret)
  4396. return ret;
  4397. raw_spin_lock_irqsave(&ctx->lock, flags);
  4398. /*
  4399. * Verify the grouping between the parent and child (inherited)
  4400. * events is still in tact.
  4401. *
  4402. * Specifically:
  4403. * - leader->ctx->lock pins leader->sibling_list
  4404. * - parent->child_mutex pins parent->child_list
  4405. * - parent->ctx->mutex pins parent->sibling_list
  4406. *
  4407. * Because parent->ctx != leader->ctx (and child_list nests inside
  4408. * ctx->mutex), group destruction is not atomic between children, also
  4409. * see perf_event_release_kernel(). Additionally, parent can grow the
  4410. * group.
  4411. *
  4412. * Therefore it is possible to have parent and child groups in a
  4413. * different configuration and summing over such a beast makes no sense
  4414. * what so ever.
  4415. *
  4416. * Reject this.
  4417. */
  4418. parent = leader->parent;
  4419. if (parent &&
  4420. (parent->group_generation != leader->group_generation ||
  4421. parent->nr_siblings != leader->nr_siblings)) {
  4422. ret = -ECHILD;
  4423. goto unlock;
  4424. }
  4425. /*
  4426. * Since we co-schedule groups, {enabled,running} times of siblings
  4427. * will be identical to those of the leader, so we only publish one
  4428. * set.
  4429. */
  4430. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  4431. values[n++] += leader->total_time_enabled +
  4432. atomic64_read(&leader->child_total_time_enabled);
  4433. }
  4434. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  4435. values[n++] += leader->total_time_running +
  4436. atomic64_read(&leader->child_total_time_running);
  4437. }
  4438. /*
  4439. * Write {count,id} tuples for every sibling.
  4440. */
  4441. values[n++] += perf_event_count(leader);
  4442. if (read_format & PERF_FORMAT_ID)
  4443. values[n++] = primary_event_id(leader);
  4444. if (read_format & PERF_FORMAT_LOST)
  4445. values[n++] = atomic64_read(&leader->lost_samples);
  4446. for_each_sibling_event(sub, leader) {
  4447. values[n++] += perf_event_count(sub);
  4448. if (read_format & PERF_FORMAT_ID)
  4449. values[n++] = primary_event_id(sub);
  4450. if (read_format & PERF_FORMAT_LOST)
  4451. values[n++] = atomic64_read(&sub->lost_samples);
  4452. }
  4453. unlock:
  4454. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  4455. return ret;
  4456. }
  4457. static int perf_read_group(struct perf_event *event,
  4458. u64 read_format, char __user *buf)
  4459. {
  4460. struct perf_event *leader = event->group_leader, *child;
  4461. struct perf_event_context *ctx = leader->ctx;
  4462. int ret;
  4463. u64 *values;
  4464. lockdep_assert_held(&ctx->mutex);
  4465. values = kzalloc(event->read_size, GFP_KERNEL);
  4466. if (!values)
  4467. return -ENOMEM;
  4468. values[0] = 1 + leader->nr_siblings;
  4469. mutex_lock(&leader->child_mutex);
  4470. ret = __perf_read_group_add(leader, read_format, values);
  4471. if (ret)
  4472. goto unlock;
  4473. list_for_each_entry(child, &leader->child_list, child_list) {
  4474. ret = __perf_read_group_add(child, read_format, values);
  4475. if (ret)
  4476. goto unlock;
  4477. }
  4478. mutex_unlock(&leader->child_mutex);
  4479. ret = event->read_size;
  4480. if (copy_to_user(buf, values, event->read_size))
  4481. ret = -EFAULT;
  4482. goto out;
  4483. unlock:
  4484. mutex_unlock(&leader->child_mutex);
  4485. out:
  4486. kfree(values);
  4487. return ret;
  4488. }
  4489. static int perf_read_one(struct perf_event *event,
  4490. u64 read_format, char __user *buf)
  4491. {
  4492. u64 enabled, running;
  4493. u64 values[5];
  4494. int n = 0;
  4495. values[n++] = __perf_event_read_value(event, &enabled, &running);
  4496. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  4497. values[n++] = enabled;
  4498. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  4499. values[n++] = running;
  4500. if (read_format & PERF_FORMAT_ID)
  4501. values[n++] = primary_event_id(event);
  4502. if (read_format & PERF_FORMAT_LOST)
  4503. values[n++] = atomic64_read(&event->lost_samples);
  4504. if (copy_to_user(buf, values, n * sizeof(u64)))
  4505. return -EFAULT;
  4506. return n * sizeof(u64);
  4507. }
  4508. static bool is_event_hup(struct perf_event *event)
  4509. {
  4510. bool no_children;
  4511. if (event->state > PERF_EVENT_STATE_EXIT)
  4512. return false;
  4513. mutex_lock(&event->child_mutex);
  4514. no_children = list_empty(&event->child_list);
  4515. mutex_unlock(&event->child_mutex);
  4516. return no_children;
  4517. }
  4518. /*
  4519. * Read the performance event - simple non blocking version for now
  4520. */
  4521. static ssize_t
  4522. __perf_read(struct perf_event *event, char __user *buf, size_t count)
  4523. {
  4524. u64 read_format = event->attr.read_format;
  4525. int ret;
  4526. /*
  4527. * Return end-of-file for a read on an event that is in
  4528. * error state (i.e. because it was pinned but it couldn't be
  4529. * scheduled on to the CPU at some point).
  4530. */
  4531. if (event->state == PERF_EVENT_STATE_ERROR)
  4532. return 0;
  4533. if (count < event->read_size)
  4534. return -ENOSPC;
  4535. WARN_ON_ONCE(event->ctx->parent_ctx);
  4536. if (read_format & PERF_FORMAT_GROUP)
  4537. ret = perf_read_group(event, read_format, buf);
  4538. else
  4539. ret = perf_read_one(event, read_format, buf);
  4540. return ret;
  4541. }
  4542. static ssize_t
  4543. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  4544. {
  4545. struct perf_event *event = file->private_data;
  4546. struct perf_event_context *ctx;
  4547. int ret;
  4548. ret = security_perf_event_read(event);
  4549. if (ret)
  4550. return ret;
  4551. ctx = perf_event_ctx_lock(event);
  4552. ret = __perf_read(event, buf, count);
  4553. perf_event_ctx_unlock(event, ctx);
  4554. return ret;
  4555. }
  4556. static __poll_t perf_poll(struct file *file, poll_table *wait)
  4557. {
  4558. struct perf_event *event = file->private_data;
  4559. struct perf_buffer *rb;
  4560. __poll_t events = EPOLLHUP;
  4561. poll_wait(file, &event->waitq, wait);
  4562. if (is_event_hup(event))
  4563. return events;
  4564. /*
  4565. * Pin the event->rb by taking event->mmap_mutex; otherwise
  4566. * perf_event_set_output() can swizzle our rb and make us miss wakeups.
  4567. */
  4568. mutex_lock(&event->mmap_mutex);
  4569. rb = event->rb;
  4570. if (rb)
  4571. events = atomic_xchg(&rb->poll, 0);
  4572. mutex_unlock(&event->mmap_mutex);
  4573. return events;
  4574. }
  4575. static void _perf_event_reset(struct perf_event *event)
  4576. {
  4577. (void)perf_event_read(event, false);
  4578. local64_set(&event->count, 0);
  4579. perf_event_update_userpage(event);
  4580. }
  4581. /* Assume it's not an event with inherit set. */
  4582. u64 perf_event_pause(struct perf_event *event, bool reset)
  4583. {
  4584. struct perf_event_context *ctx;
  4585. u64 count;
  4586. ctx = perf_event_ctx_lock(event);
  4587. WARN_ON_ONCE(event->attr.inherit);
  4588. _perf_event_disable(event);
  4589. count = local64_read(&event->count);
  4590. if (reset)
  4591. local64_set(&event->count, 0);
  4592. perf_event_ctx_unlock(event, ctx);
  4593. return count;
  4594. }
  4595. EXPORT_SYMBOL_GPL(perf_event_pause);
  4596. /*
  4597. * Holding the top-level event's child_mutex means that any
  4598. * descendant process that has inherited this event will block
  4599. * in perf_event_exit_event() if it goes to exit, thus satisfying the
  4600. * task existence requirements of perf_event_enable/disable.
  4601. */
  4602. static void perf_event_for_each_child(struct perf_event *event,
  4603. void (*func)(struct perf_event *))
  4604. {
  4605. struct perf_event *child;
  4606. WARN_ON_ONCE(event->ctx->parent_ctx);
  4607. mutex_lock(&event->child_mutex);
  4608. func(event);
  4609. list_for_each_entry(child, &event->child_list, child_list)
  4610. func(child);
  4611. mutex_unlock(&event->child_mutex);
  4612. }
  4613. static void perf_event_for_each(struct perf_event *event,
  4614. void (*func)(struct perf_event *))
  4615. {
  4616. struct perf_event_context *ctx = event->ctx;
  4617. struct perf_event *sibling;
  4618. lockdep_assert_held(&ctx->mutex);
  4619. event = event->group_leader;
  4620. perf_event_for_each_child(event, func);
  4621. for_each_sibling_event(sibling, event)
  4622. perf_event_for_each_child(sibling, func);
  4623. }
  4624. static void __perf_event_period(struct perf_event *event,
  4625. struct perf_cpu_context *cpuctx,
  4626. struct perf_event_context *ctx,
  4627. void *info)
  4628. {
  4629. u64 value = *((u64 *)info);
  4630. bool active;
  4631. if (event->attr.freq) {
  4632. event->attr.sample_freq = value;
  4633. } else {
  4634. event->attr.sample_period = value;
  4635. event->hw.sample_period = value;
  4636. }
  4637. active = (event->state == PERF_EVENT_STATE_ACTIVE);
  4638. if (active) {
  4639. perf_pmu_disable(ctx->pmu);
  4640. /*
  4641. * We could be throttled; unthrottle now to avoid the tick
  4642. * trying to unthrottle while we already re-started the event.
  4643. */
  4644. if (event->hw.interrupts == MAX_INTERRUPTS) {
  4645. event->hw.interrupts = 0;
  4646. perf_log_throttle(event, 1);
  4647. }
  4648. event->pmu->stop(event, PERF_EF_UPDATE);
  4649. }
  4650. local64_set(&event->hw.period_left, 0);
  4651. if (active) {
  4652. event->pmu->start(event, PERF_EF_RELOAD);
  4653. perf_pmu_enable(ctx->pmu);
  4654. }
  4655. }
  4656. static int perf_event_check_period(struct perf_event *event, u64 value)
  4657. {
  4658. return event->pmu->check_period(event, value);
  4659. }
  4660. static int _perf_event_period(struct perf_event *event, u64 value)
  4661. {
  4662. if (!is_sampling_event(event))
  4663. return -EINVAL;
  4664. if (!value)
  4665. return -EINVAL;
  4666. if (event->attr.freq && value > sysctl_perf_event_sample_rate)
  4667. return -EINVAL;
  4668. if (perf_event_check_period(event, value))
  4669. return -EINVAL;
  4670. if (!event->attr.freq && (value & (1ULL << 63)))
  4671. return -EINVAL;
  4672. event_function_call(event, __perf_event_period, &value);
  4673. return 0;
  4674. }
  4675. int perf_event_period(struct perf_event *event, u64 value)
  4676. {
  4677. struct perf_event_context *ctx;
  4678. int ret;
  4679. ctx = perf_event_ctx_lock(event);
  4680. ret = _perf_event_period(event, value);
  4681. perf_event_ctx_unlock(event, ctx);
  4682. return ret;
  4683. }
  4684. EXPORT_SYMBOL_GPL(perf_event_period);
  4685. static const struct file_operations perf_fops;
  4686. static inline int perf_fget_light(int fd, struct fd *p)
  4687. {
  4688. struct fd f = fdget(fd);
  4689. if (!f.file)
  4690. return -EBADF;
  4691. if (f.file->f_op != &perf_fops) {
  4692. fdput(f);
  4693. return -EBADF;
  4694. }
  4695. *p = f;
  4696. return 0;
  4697. }
  4698. static int perf_event_set_output(struct perf_event *event,
  4699. struct perf_event *output_event);
  4700. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  4701. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4702. struct perf_event_attr *attr);
  4703. static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
  4704. {
  4705. void (*func)(struct perf_event *);
  4706. u32 flags = arg;
  4707. switch (cmd) {
  4708. case PERF_EVENT_IOC_ENABLE:
  4709. func = _perf_event_enable;
  4710. break;
  4711. case PERF_EVENT_IOC_DISABLE:
  4712. func = _perf_event_disable;
  4713. break;
  4714. case PERF_EVENT_IOC_RESET:
  4715. func = _perf_event_reset;
  4716. break;
  4717. case PERF_EVENT_IOC_REFRESH:
  4718. return _perf_event_refresh(event, arg);
  4719. case PERF_EVENT_IOC_PERIOD:
  4720. {
  4721. u64 value;
  4722. if (copy_from_user(&value, (u64 __user *)arg, sizeof(value)))
  4723. return -EFAULT;
  4724. return _perf_event_period(event, value);
  4725. }
  4726. case PERF_EVENT_IOC_ID:
  4727. {
  4728. u64 id = primary_event_id(event);
  4729. if (copy_to_user((void __user *)arg, &id, sizeof(id)))
  4730. return -EFAULT;
  4731. return 0;
  4732. }
  4733. case PERF_EVENT_IOC_SET_OUTPUT:
  4734. {
  4735. int ret;
  4736. if (arg != -1) {
  4737. struct perf_event *output_event;
  4738. struct fd output;
  4739. ret = perf_fget_light(arg, &output);
  4740. if (ret)
  4741. return ret;
  4742. output_event = output.file->private_data;
  4743. ret = perf_event_set_output(event, output_event);
  4744. fdput(output);
  4745. } else {
  4746. ret = perf_event_set_output(event, NULL);
  4747. }
  4748. return ret;
  4749. }
  4750. case PERF_EVENT_IOC_SET_FILTER:
  4751. return perf_event_set_filter(event, (void __user *)arg);
  4752. case PERF_EVENT_IOC_SET_BPF:
  4753. {
  4754. struct bpf_prog *prog;
  4755. int err;
  4756. prog = bpf_prog_get(arg);
  4757. if (IS_ERR(prog))
  4758. return PTR_ERR(prog);
  4759. err = perf_event_set_bpf_prog(event, prog, 0);
  4760. if (err) {
  4761. bpf_prog_put(prog);
  4762. return err;
  4763. }
  4764. return 0;
  4765. }
  4766. case PERF_EVENT_IOC_PAUSE_OUTPUT: {
  4767. struct perf_buffer *rb;
  4768. rcu_read_lock();
  4769. rb = rcu_dereference(event->rb);
  4770. if (!rb || !rb->nr_pages) {
  4771. rcu_read_unlock();
  4772. return -EINVAL;
  4773. }
  4774. rb_toggle_paused(rb, !!arg);
  4775. rcu_read_unlock();
  4776. return 0;
  4777. }
  4778. case PERF_EVENT_IOC_QUERY_BPF:
  4779. return perf_event_query_prog_array(event, (void __user *)arg);
  4780. case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
  4781. struct perf_event_attr new_attr;
  4782. int err = perf_copy_attr((struct perf_event_attr __user *)arg,
  4783. &new_attr);
  4784. if (err)
  4785. return err;
  4786. return perf_event_modify_attr(event, &new_attr);
  4787. }
  4788. default:
  4789. return -ENOTTY;
  4790. }
  4791. if (flags & PERF_IOC_FLAG_GROUP)
  4792. perf_event_for_each(event, func);
  4793. else
  4794. perf_event_for_each_child(event, func);
  4795. return 0;
  4796. }
  4797. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  4798. {
  4799. struct perf_event *event = file->private_data;
  4800. struct perf_event_context *ctx;
  4801. long ret;
  4802. /* Treat ioctl like writes as it is likely a mutating operation. */
  4803. ret = security_perf_event_write(event);
  4804. if (ret)
  4805. return ret;
  4806. ctx = perf_event_ctx_lock(event);
  4807. ret = _perf_ioctl(event, cmd, arg);
  4808. perf_event_ctx_unlock(event, ctx);
  4809. return ret;
  4810. }
  4811. #ifdef CONFIG_COMPAT
  4812. static long perf_compat_ioctl(struct file *file, unsigned int cmd,
  4813. unsigned long arg)
  4814. {
  4815. switch (_IOC_NR(cmd)) {
  4816. case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
  4817. case _IOC_NR(PERF_EVENT_IOC_ID):
  4818. case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF):
  4819. case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES):
  4820. /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
  4821. if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
  4822. cmd &= ~IOCSIZE_MASK;
  4823. cmd |= sizeof(void *) << IOCSIZE_SHIFT;
  4824. }
  4825. break;
  4826. }
  4827. return perf_ioctl(file, cmd, arg);
  4828. }
  4829. #else
  4830. # define perf_compat_ioctl NULL
  4831. #endif
  4832. int perf_event_task_enable(void)
  4833. {
  4834. struct perf_event_context *ctx;
  4835. struct perf_event *event;
  4836. mutex_lock(&current->perf_event_mutex);
  4837. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  4838. ctx = perf_event_ctx_lock(event);
  4839. perf_event_for_each_child(event, _perf_event_enable);
  4840. perf_event_ctx_unlock(event, ctx);
  4841. }
  4842. mutex_unlock(&current->perf_event_mutex);
  4843. return 0;
  4844. }
  4845. int perf_event_task_disable(void)
  4846. {
  4847. struct perf_event_context *ctx;
  4848. struct perf_event *event;
  4849. mutex_lock(&current->perf_event_mutex);
  4850. list_for_each_entry(event, &current->perf_event_list, owner_entry) {
  4851. ctx = perf_event_ctx_lock(event);
  4852. perf_event_for_each_child(event, _perf_event_disable);
  4853. perf_event_ctx_unlock(event, ctx);
  4854. }
  4855. mutex_unlock(&current->perf_event_mutex);
  4856. return 0;
  4857. }
  4858. static int perf_event_index(struct perf_event *event)
  4859. {
  4860. if (event->hw.state & PERF_HES_STOPPED)
  4861. return 0;
  4862. if (event->state != PERF_EVENT_STATE_ACTIVE)
  4863. return 0;
  4864. return event->pmu->event_idx(event);
  4865. }
  4866. static void perf_event_init_userpage(struct perf_event *event)
  4867. {
  4868. struct perf_event_mmap_page *userpg;
  4869. struct perf_buffer *rb;
  4870. rcu_read_lock();
  4871. rb = rcu_dereference(event->rb);
  4872. if (!rb)
  4873. goto unlock;
  4874. userpg = rb->user_page;
  4875. /* Allow new userspace to detect that bit 0 is deprecated */
  4876. userpg->cap_bit0_is_deprecated = 1;
  4877. userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
  4878. userpg->data_offset = PAGE_SIZE;
  4879. userpg->data_size = perf_data_size(rb);
  4880. unlock:
  4881. rcu_read_unlock();
  4882. }
  4883. void __weak arch_perf_update_userpage(
  4884. struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
  4885. {
  4886. }
  4887. /*
  4888. * Callers need to ensure there can be no nesting of this function, otherwise
  4889. * the seqlock logic goes bad. We can not serialize this because the arch
  4890. * code calls this from NMI context.
  4891. */
  4892. void perf_event_update_userpage(struct perf_event *event)
  4893. {
  4894. struct perf_event_mmap_page *userpg;
  4895. struct perf_buffer *rb;
  4896. u64 enabled, running, now;
  4897. rcu_read_lock();
  4898. rb = rcu_dereference(event->rb);
  4899. if (!rb)
  4900. goto unlock;
  4901. /*
  4902. * compute total_time_enabled, total_time_running
  4903. * based on snapshot values taken when the event
  4904. * was last scheduled in.
  4905. *
  4906. * we cannot simply called update_context_time()
  4907. * because of locking issue as we can be called in
  4908. * NMI context
  4909. */
  4910. calc_timer_values(event, &now, &enabled, &running);
  4911. userpg = rb->user_page;
  4912. /*
  4913. * Disable preemption to guarantee consistent time stamps are stored to
  4914. * the user page.
  4915. */
  4916. preempt_disable();
  4917. ++userpg->lock;
  4918. barrier();
  4919. userpg->index = perf_event_index(event);
  4920. userpg->offset = perf_event_count(event);
  4921. if (userpg->index)
  4922. userpg->offset -= local64_read(&event->hw.prev_count);
  4923. userpg->time_enabled = enabled +
  4924. atomic64_read(&event->child_total_time_enabled);
  4925. userpg->time_running = running +
  4926. atomic64_read(&event->child_total_time_running);
  4927. arch_perf_update_userpage(event, userpg, now);
  4928. barrier();
  4929. ++userpg->lock;
  4930. preempt_enable();
  4931. unlock:
  4932. rcu_read_unlock();
  4933. }
  4934. EXPORT_SYMBOL_GPL(perf_event_update_userpage);
  4935. static vm_fault_t perf_mmap_fault(struct vm_fault *vmf)
  4936. {
  4937. struct perf_event *event = vmf->vma->vm_file->private_data;
  4938. struct perf_buffer *rb;
  4939. vm_fault_t ret = VM_FAULT_SIGBUS;
  4940. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  4941. if (vmf->pgoff == 0)
  4942. ret = 0;
  4943. return ret;
  4944. }
  4945. rcu_read_lock();
  4946. rb = rcu_dereference(event->rb);
  4947. if (!rb)
  4948. goto unlock;
  4949. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  4950. goto unlock;
  4951. vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
  4952. if (!vmf->page)
  4953. goto unlock;
  4954. get_page(vmf->page);
  4955. vmf->page->mapping = vmf->vma->vm_file->f_mapping;
  4956. vmf->page->index = vmf->pgoff;
  4957. ret = 0;
  4958. unlock:
  4959. rcu_read_unlock();
  4960. return ret;
  4961. }
  4962. static void ring_buffer_attach(struct perf_event *event,
  4963. struct perf_buffer *rb)
  4964. {
  4965. struct perf_buffer *old_rb = NULL;
  4966. unsigned long flags;
  4967. WARN_ON_ONCE(event->parent);
  4968. if (event->rb) {
  4969. /*
  4970. * Should be impossible, we set this when removing
  4971. * event->rb_entry and wait/clear when adding event->rb_entry.
  4972. */
  4973. WARN_ON_ONCE(event->rcu_pending);
  4974. old_rb = event->rb;
  4975. spin_lock_irqsave(&old_rb->event_lock, flags);
  4976. list_del_rcu(&event->rb_entry);
  4977. spin_unlock_irqrestore(&old_rb->event_lock, flags);
  4978. event->rcu_batches = get_state_synchronize_rcu();
  4979. event->rcu_pending = 1;
  4980. }
  4981. if (rb) {
  4982. if (event->rcu_pending) {
  4983. cond_synchronize_rcu(event->rcu_batches);
  4984. event->rcu_pending = 0;
  4985. }
  4986. spin_lock_irqsave(&rb->event_lock, flags);
  4987. list_add_rcu(&event->rb_entry, &rb->event_list);
  4988. spin_unlock_irqrestore(&rb->event_lock, flags);
  4989. }
  4990. /*
  4991. * Avoid racing with perf_mmap_close(AUX): stop the event
  4992. * before swizzling the event::rb pointer; if it's getting
  4993. * unmapped, its aux_mmap_count will be 0 and it won't
  4994. * restart. See the comment in __perf_pmu_output_stop().
  4995. *
  4996. * Data will inevitably be lost when set_output is done in
  4997. * mid-air, but then again, whoever does it like this is
  4998. * not in for the data anyway.
  4999. */
  5000. if (has_aux(event))
  5001. perf_event_stop(event, 0);
  5002. rcu_assign_pointer(event->rb, rb);
  5003. if (old_rb) {
  5004. ring_buffer_put(old_rb);
  5005. /*
  5006. * Since we detached before setting the new rb, so that we
  5007. * could attach the new rb, we could have missed a wakeup.
  5008. * Provide it now.
  5009. */
  5010. wake_up_all(&event->waitq);
  5011. }
  5012. }
  5013. static void ring_buffer_wakeup(struct perf_event *event)
  5014. {
  5015. struct perf_buffer *rb;
  5016. if (event->parent)
  5017. event = event->parent;
  5018. rcu_read_lock();
  5019. rb = rcu_dereference(event->rb);
  5020. if (rb) {
  5021. list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
  5022. wake_up_all(&event->waitq);
  5023. }
  5024. rcu_read_unlock();
  5025. }
  5026. struct perf_buffer *ring_buffer_get(struct perf_event *event)
  5027. {
  5028. struct perf_buffer *rb;
  5029. if (event->parent)
  5030. event = event->parent;
  5031. rcu_read_lock();
  5032. rb = rcu_dereference(event->rb);
  5033. if (rb) {
  5034. if (!refcount_inc_not_zero(&rb->refcount))
  5035. rb = NULL;
  5036. }
  5037. rcu_read_unlock();
  5038. return rb;
  5039. }
  5040. void ring_buffer_put(struct perf_buffer *rb)
  5041. {
  5042. if (!refcount_dec_and_test(&rb->refcount))
  5043. return;
  5044. WARN_ON_ONCE(!list_empty(&rb->event_list));
  5045. call_rcu(&rb->rcu_head, rb_free_rcu);
  5046. }
  5047. static void perf_mmap_open(struct vm_area_struct *vma)
  5048. {
  5049. struct perf_event *event = vma->vm_file->private_data;
  5050. atomic_inc(&event->mmap_count);
  5051. atomic_inc(&event->rb->mmap_count);
  5052. if (vma->vm_pgoff)
  5053. atomic_inc(&event->rb->aux_mmap_count);
  5054. if (event->pmu->event_mapped)
  5055. event->pmu->event_mapped(event, vma->vm_mm);
  5056. }
  5057. static void perf_pmu_output_stop(struct perf_event *event);
  5058. /*
  5059. * A buffer can be mmap()ed multiple times; either directly through the same
  5060. * event, or through other events by use of perf_event_set_output().
  5061. *
  5062. * In order to undo the VM accounting done by perf_mmap() we need to destroy
  5063. * the buffer here, where we still have a VM context. This means we need
  5064. * to detach all events redirecting to us.
  5065. */
  5066. static void perf_mmap_close(struct vm_area_struct *vma)
  5067. {
  5068. struct perf_event *event = vma->vm_file->private_data;
  5069. struct perf_buffer *rb = ring_buffer_get(event);
  5070. struct user_struct *mmap_user = rb->mmap_user;
  5071. int mmap_locked = rb->mmap_locked;
  5072. unsigned long size = perf_data_size(rb);
  5073. bool detach_rest = false;
  5074. if (event->pmu->event_unmapped)
  5075. event->pmu->event_unmapped(event, vma->vm_mm);
  5076. /*
  5077. * rb->aux_mmap_count will always drop before rb->mmap_count and
  5078. * event->mmap_count, so it is ok to use event->mmap_mutex to
  5079. * serialize with perf_mmap here.
  5080. */
  5081. if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
  5082. atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
  5083. /*
  5084. * Stop all AUX events that are writing to this buffer,
  5085. * so that we can free its AUX pages and corresponding PMU
  5086. * data. Note that after rb::aux_mmap_count dropped to zero,
  5087. * they won't start any more (see perf_aux_output_begin()).
  5088. */
  5089. perf_pmu_output_stop(event);
  5090. /* now it's safe to free the pages */
  5091. atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm);
  5092. atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm);
  5093. /* this has to be the last one */
  5094. rb_free_aux(rb);
  5095. WARN_ON_ONCE(refcount_read(&rb->aux_refcount));
  5096. mutex_unlock(&event->mmap_mutex);
  5097. }
  5098. if (atomic_dec_and_test(&rb->mmap_count))
  5099. detach_rest = true;
  5100. if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
  5101. goto out_put;
  5102. ring_buffer_attach(event, NULL);
  5103. mutex_unlock(&event->mmap_mutex);
  5104. /* If there's still other mmap()s of this buffer, we're done. */
  5105. if (!detach_rest)
  5106. goto out_put;
  5107. /*
  5108. * No other mmap()s, detach from all other events that might redirect
  5109. * into the now unreachable buffer. Somewhat complicated by the
  5110. * fact that rb::event_lock otherwise nests inside mmap_mutex.
  5111. */
  5112. again:
  5113. rcu_read_lock();
  5114. list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
  5115. if (!atomic_long_inc_not_zero(&event->refcount)) {
  5116. /*
  5117. * This event is en-route to free_event() which will
  5118. * detach it and remove it from the list.
  5119. */
  5120. continue;
  5121. }
  5122. rcu_read_unlock();
  5123. mutex_lock(&event->mmap_mutex);
  5124. /*
  5125. * Check we didn't race with perf_event_set_output() which can
  5126. * swizzle the rb from under us while we were waiting to
  5127. * acquire mmap_mutex.
  5128. *
  5129. * If we find a different rb; ignore this event, a next
  5130. * iteration will no longer find it on the list. We have to
  5131. * still restart the iteration to make sure we're not now
  5132. * iterating the wrong list.
  5133. */
  5134. if (event->rb == rb)
  5135. ring_buffer_attach(event, NULL);
  5136. mutex_unlock(&event->mmap_mutex);
  5137. put_event(event);
  5138. /*
  5139. * Restart the iteration; either we're on the wrong list or
  5140. * destroyed its integrity by doing a deletion.
  5141. */
  5142. goto again;
  5143. }
  5144. rcu_read_unlock();
  5145. /*
  5146. * It could be there's still a few 0-ref events on the list; they'll
  5147. * get cleaned up by free_event() -- they'll also still have their
  5148. * ref on the rb and will free it whenever they are done with it.
  5149. *
  5150. * Aside from that, this buffer is 'fully' detached and unmapped,
  5151. * undo the VM accounting.
  5152. */
  5153. atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked,
  5154. &mmap_user->locked_vm);
  5155. atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm);
  5156. free_uid(mmap_user);
  5157. out_put:
  5158. ring_buffer_put(rb); /* could be last */
  5159. }
  5160. static const struct vm_operations_struct perf_mmap_vmops = {
  5161. .open = perf_mmap_open,
  5162. .close = perf_mmap_close, /* non mergeable */
  5163. .fault = perf_mmap_fault,
  5164. .page_mkwrite = perf_mmap_fault,
  5165. };
  5166. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  5167. {
  5168. struct perf_event *event = file->private_data;
  5169. unsigned long user_locked, user_lock_limit;
  5170. struct user_struct *user = current_user();
  5171. struct perf_buffer *rb = NULL;
  5172. unsigned long locked, lock_limit;
  5173. unsigned long vma_size;
  5174. unsigned long nr_pages;
  5175. long user_extra = 0, extra = 0;
  5176. int ret = 0, flags = 0;
  5177. /*
  5178. * Don't allow mmap() of inherited per-task counters. This would
  5179. * create a performance issue due to all children writing to the
  5180. * same rb.
  5181. */
  5182. if (event->cpu == -1 && event->attr.inherit)
  5183. return -EINVAL;
  5184. if (!(vma->vm_flags & VM_SHARED))
  5185. return -EINVAL;
  5186. ret = security_perf_event_read(event);
  5187. if (ret)
  5188. return ret;
  5189. vma_size = vma->vm_end - vma->vm_start;
  5190. if (vma->vm_pgoff == 0) {
  5191. nr_pages = (vma_size / PAGE_SIZE) - 1;
  5192. } else {
  5193. /*
  5194. * AUX area mapping: if rb->aux_nr_pages != 0, it's already
  5195. * mapped, all subsequent mappings should have the same size
  5196. * and offset. Must be above the normal perf buffer.
  5197. */
  5198. u64 aux_offset, aux_size;
  5199. if (!event->rb)
  5200. return -EINVAL;
  5201. nr_pages = vma_size / PAGE_SIZE;
  5202. mutex_lock(&event->mmap_mutex);
  5203. ret = -EINVAL;
  5204. rb = event->rb;
  5205. if (!rb)
  5206. goto aux_unlock;
  5207. aux_offset = READ_ONCE(rb->user_page->aux_offset);
  5208. aux_size = READ_ONCE(rb->user_page->aux_size);
  5209. if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
  5210. goto aux_unlock;
  5211. if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
  5212. goto aux_unlock;
  5213. /* already mapped with a different offset */
  5214. if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
  5215. goto aux_unlock;
  5216. if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
  5217. goto aux_unlock;
  5218. /* already mapped with a different size */
  5219. if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
  5220. goto aux_unlock;
  5221. if (!is_power_of_2(nr_pages))
  5222. goto aux_unlock;
  5223. if (!atomic_inc_not_zero(&rb->mmap_count))
  5224. goto aux_unlock;
  5225. if (rb_has_aux(rb)) {
  5226. atomic_inc(&rb->aux_mmap_count);
  5227. ret = 0;
  5228. goto unlock;
  5229. }
  5230. atomic_set(&rb->aux_mmap_count, 1);
  5231. user_extra = nr_pages;
  5232. goto accounting;
  5233. }
  5234. /*
  5235. * If we have rb pages ensure they're a power-of-two number, so we
  5236. * can do bitmasks instead of modulo.
  5237. */
  5238. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  5239. return -EINVAL;
  5240. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  5241. return -EINVAL;
  5242. WARN_ON_ONCE(event->ctx->parent_ctx);
  5243. again:
  5244. mutex_lock(&event->mmap_mutex);
  5245. if (event->rb) {
  5246. if (data_page_nr(event->rb) != nr_pages) {
  5247. ret = -EINVAL;
  5248. goto unlock;
  5249. }
  5250. if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
  5251. /*
  5252. * Raced against perf_mmap_close(); remove the
  5253. * event and try again.
  5254. */
  5255. ring_buffer_attach(event, NULL);
  5256. mutex_unlock(&event->mmap_mutex);
  5257. goto again;
  5258. }
  5259. goto unlock;
  5260. }
  5261. user_extra = nr_pages + 1;
  5262. accounting:
  5263. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  5264. /*
  5265. * Increase the limit linearly with more CPUs:
  5266. */
  5267. user_lock_limit *= num_online_cpus();
  5268. user_locked = atomic_long_read(&user->locked_vm);
  5269. /*
  5270. * sysctl_perf_event_mlock may have changed, so that
  5271. * user->locked_vm > user_lock_limit
  5272. */
  5273. if (user_locked > user_lock_limit)
  5274. user_locked = user_lock_limit;
  5275. user_locked += user_extra;
  5276. if (user_locked > user_lock_limit) {
  5277. /*
  5278. * charge locked_vm until it hits user_lock_limit;
  5279. * charge the rest from pinned_vm
  5280. */
  5281. extra = user_locked - user_lock_limit;
  5282. user_extra -= extra;
  5283. }
  5284. lock_limit = rlimit(RLIMIT_MEMLOCK);
  5285. lock_limit >>= PAGE_SHIFT;
  5286. locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra;
  5287. if ((locked > lock_limit) && perf_is_paranoid() &&
  5288. !capable(CAP_IPC_LOCK)) {
  5289. ret = -EPERM;
  5290. goto unlock;
  5291. }
  5292. WARN_ON(!rb && event->rb);
  5293. if (vma->vm_flags & VM_WRITE)
  5294. flags |= RING_BUFFER_WRITABLE;
  5295. if (!rb) {
  5296. rb = rb_alloc(nr_pages,
  5297. event->attr.watermark ? event->attr.wakeup_watermark : 0,
  5298. event->cpu, flags);
  5299. if (!rb) {
  5300. ret = -ENOMEM;
  5301. goto unlock;
  5302. }
  5303. atomic_set(&rb->mmap_count, 1);
  5304. rb->mmap_user = get_current_user();
  5305. rb->mmap_locked = extra;
  5306. ring_buffer_attach(event, rb);
  5307. perf_event_update_time(event);
  5308. perf_event_init_userpage(event);
  5309. perf_event_update_userpage(event);
  5310. } else {
  5311. ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
  5312. event->attr.aux_watermark, flags);
  5313. if (!ret)
  5314. rb->aux_mmap_locked = extra;
  5315. }
  5316. unlock:
  5317. if (!ret) {
  5318. atomic_long_add(user_extra, &user->locked_vm);
  5319. atomic64_add(extra, &vma->vm_mm->pinned_vm);
  5320. atomic_inc(&event->mmap_count);
  5321. } else if (rb) {
  5322. atomic_dec(&rb->mmap_count);
  5323. }
  5324. aux_unlock:
  5325. mutex_unlock(&event->mmap_mutex);
  5326. /*
  5327. * Since pinned accounting is per vm we cannot allow fork() to copy our
  5328. * vma.
  5329. */
  5330. vm_flags_set(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP);
  5331. vma->vm_ops = &perf_mmap_vmops;
  5332. if (event->pmu->event_mapped)
  5333. event->pmu->event_mapped(event, vma->vm_mm);
  5334. return ret;
  5335. }
  5336. static int perf_fasync(int fd, struct file *filp, int on)
  5337. {
  5338. struct inode *inode = file_inode(filp);
  5339. struct perf_event *event = filp->private_data;
  5340. int retval;
  5341. inode_lock(inode);
  5342. retval = fasync_helper(fd, filp, on, &event->fasync);
  5343. inode_unlock(inode);
  5344. if (retval < 0)
  5345. return retval;
  5346. return 0;
  5347. }
  5348. static const struct file_operations perf_fops = {
  5349. .llseek = no_llseek,
  5350. .release = perf_release,
  5351. .read = perf_read,
  5352. .poll = perf_poll,
  5353. .unlocked_ioctl = perf_ioctl,
  5354. .compat_ioctl = perf_compat_ioctl,
  5355. .mmap = perf_mmap,
  5356. .fasync = perf_fasync,
  5357. };
  5358. /*
  5359. * Perf event wakeup
  5360. *
  5361. * If there's data, ensure we set the poll() state and publish everything
  5362. * to user-space before waking everybody up.
  5363. */
  5364. static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
  5365. {
  5366. /* only the parent has fasync state */
  5367. if (event->parent)
  5368. event = event->parent;
  5369. return &event->fasync;
  5370. }
  5371. void perf_event_wakeup(struct perf_event *event)
  5372. {
  5373. ring_buffer_wakeup(event);
  5374. if (event->pending_kill) {
  5375. kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
  5376. event->pending_kill = 0;
  5377. }
  5378. }
  5379. static void perf_sigtrap(struct perf_event *event)
  5380. {
  5381. /*
  5382. * We'd expect this to only occur if the irq_work is delayed and either
  5383. * ctx->task or current has changed in the meantime. This can be the
  5384. * case on architectures that do not implement arch_irq_work_raise().
  5385. */
  5386. if (WARN_ON_ONCE(event->ctx->task != current))
  5387. return;
  5388. /*
  5389. * Both perf_pending_task() and perf_pending_irq() can race with the
  5390. * task exiting.
  5391. */
  5392. if (current->flags & PF_EXITING)
  5393. return;
  5394. send_sig_perf((void __user *)event->pending_addr,
  5395. event->attr.type, event->attr.sig_data);
  5396. }
  5397. /*
  5398. * Deliver the pending work in-event-context or follow the context.
  5399. */
  5400. static void __perf_pending_irq(struct perf_event *event)
  5401. {
  5402. int cpu = READ_ONCE(event->oncpu);
  5403. /*
  5404. * If the event isn't running; we done. event_sched_out() will have
  5405. * taken care of things.
  5406. */
  5407. if (cpu < 0)
  5408. return;
  5409. /*
  5410. * Yay, we hit home and are in the context of the event.
  5411. */
  5412. if (cpu == smp_processor_id()) {
  5413. if (event->pending_sigtrap) {
  5414. event->pending_sigtrap = 0;
  5415. perf_sigtrap(event);
  5416. local_dec(&event->ctx->nr_pending);
  5417. }
  5418. if (event->pending_disable) {
  5419. event->pending_disable = 0;
  5420. perf_event_disable_local(event);
  5421. }
  5422. return;
  5423. }
  5424. /*
  5425. * CPU-A CPU-B
  5426. *
  5427. * perf_event_disable_inatomic()
  5428. * @pending_disable = CPU-A;
  5429. * irq_work_queue();
  5430. *
  5431. * sched-out
  5432. * @pending_disable = -1;
  5433. *
  5434. * sched-in
  5435. * perf_event_disable_inatomic()
  5436. * @pending_disable = CPU-B;
  5437. * irq_work_queue(); // FAILS
  5438. *
  5439. * irq_work_run()
  5440. * perf_pending_irq()
  5441. *
  5442. * But the event runs on CPU-B and wants disabling there.
  5443. */
  5444. irq_work_queue_on(&event->pending_irq, cpu);
  5445. }
  5446. static void perf_pending_irq(struct irq_work *entry)
  5447. {
  5448. struct perf_event *event = container_of(entry, struct perf_event, pending_irq);
  5449. int rctx;
  5450. /*
  5451. * If we 'fail' here, that's OK, it means recursion is already disabled
  5452. * and we won't recurse 'further'.
  5453. */
  5454. rctx = perf_swevent_get_recursion_context();
  5455. /*
  5456. * The wakeup isn't bound to the context of the event -- it can happen
  5457. * irrespective of where the event is.
  5458. */
  5459. if (event->pending_wakeup) {
  5460. event->pending_wakeup = 0;
  5461. perf_event_wakeup(event);
  5462. }
  5463. __perf_pending_irq(event);
  5464. if (rctx >= 0)
  5465. perf_swevent_put_recursion_context(rctx);
  5466. }
  5467. static void perf_pending_task(struct callback_head *head)
  5468. {
  5469. struct perf_event *event = container_of(head, struct perf_event, pending_task);
  5470. int rctx;
  5471. /*
  5472. * If we 'fail' here, that's OK, it means recursion is already disabled
  5473. * and we won't recurse 'further'.
  5474. */
  5475. preempt_disable_notrace();
  5476. rctx = perf_swevent_get_recursion_context();
  5477. if (event->pending_work) {
  5478. event->pending_work = 0;
  5479. perf_sigtrap(event);
  5480. local_dec(&event->ctx->nr_pending);
  5481. }
  5482. if (rctx >= 0)
  5483. perf_swevent_put_recursion_context(rctx);
  5484. preempt_enable_notrace();
  5485. put_event(event);
  5486. }
  5487. #ifdef CONFIG_GUEST_PERF_EVENTS
  5488. struct perf_guest_info_callbacks __rcu *perf_guest_cbs;
  5489. void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  5490. {
  5491. if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs)))
  5492. return;
  5493. rcu_assign_pointer(perf_guest_cbs, cbs);
  5494. }
  5495. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  5496. void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  5497. {
  5498. if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs) != cbs))
  5499. return;
  5500. rcu_assign_pointer(perf_guest_cbs, NULL);
  5501. synchronize_rcu();
  5502. }
  5503. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  5504. #endif
  5505. static void
  5506. perf_output_sample_regs(struct perf_output_handle *handle,
  5507. struct pt_regs *regs, u64 mask)
  5508. {
  5509. int bit;
  5510. DECLARE_BITMAP(_mask, 64);
  5511. bitmap_from_u64(_mask, mask);
  5512. for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
  5513. u64 val;
  5514. val = perf_reg_value(regs, bit);
  5515. perf_output_put(handle, val);
  5516. }
  5517. }
  5518. static void perf_sample_regs_user(struct perf_regs *regs_user,
  5519. struct pt_regs *regs)
  5520. {
  5521. if (user_mode(regs)) {
  5522. regs_user->abi = perf_reg_abi(current);
  5523. regs_user->regs = regs;
  5524. } else if (!(current->flags & PF_KTHREAD)) {
  5525. perf_get_regs_user(regs_user, regs);
  5526. } else {
  5527. regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
  5528. regs_user->regs = NULL;
  5529. }
  5530. }
  5531. static void perf_sample_regs_intr(struct perf_regs *regs_intr,
  5532. struct pt_regs *regs)
  5533. {
  5534. regs_intr->regs = regs;
  5535. regs_intr->abi = perf_reg_abi(current);
  5536. }
  5537. /*
  5538. * Get remaining task size from user stack pointer.
  5539. *
  5540. * It'd be better to take stack vma map and limit this more
  5541. * precisely, but there's no way to get it safely under interrupt,
  5542. * so using TASK_SIZE as limit.
  5543. */
  5544. static u64 perf_ustack_task_size(struct pt_regs *regs)
  5545. {
  5546. unsigned long addr = perf_user_stack_pointer(regs);
  5547. if (!addr || addr >= TASK_SIZE)
  5548. return 0;
  5549. return TASK_SIZE - addr;
  5550. }
  5551. static u16
  5552. perf_sample_ustack_size(u16 stack_size, u16 header_size,
  5553. struct pt_regs *regs)
  5554. {
  5555. u64 task_size;
  5556. /* No regs, no stack pointer, no dump. */
  5557. if (!regs)
  5558. return 0;
  5559. /*
  5560. * Check if we fit in with the requested stack size into the:
  5561. * - TASK_SIZE
  5562. * If we don't, we limit the size to the TASK_SIZE.
  5563. *
  5564. * - remaining sample size
  5565. * If we don't, we customize the stack size to
  5566. * fit in to the remaining sample size.
  5567. */
  5568. task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
  5569. stack_size = min(stack_size, (u16) task_size);
  5570. /* Current header size plus static size and dynamic size. */
  5571. header_size += 2 * sizeof(u64);
  5572. /* Do we fit in with the current stack dump size? */
  5573. if ((u16) (header_size + stack_size) < header_size) {
  5574. /*
  5575. * If we overflow the maximum size for the sample,
  5576. * we customize the stack dump size to fit in.
  5577. */
  5578. stack_size = USHRT_MAX - header_size - sizeof(u64);
  5579. stack_size = round_up(stack_size, sizeof(u64));
  5580. }
  5581. return stack_size;
  5582. }
  5583. static void
  5584. perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
  5585. struct pt_regs *regs)
  5586. {
  5587. /* Case of a kernel thread, nothing to dump */
  5588. if (!regs) {
  5589. u64 size = 0;
  5590. perf_output_put(handle, size);
  5591. } else {
  5592. unsigned long sp;
  5593. unsigned int rem;
  5594. u64 dyn_size;
  5595. /*
  5596. * We dump:
  5597. * static size
  5598. * - the size requested by user or the best one we can fit
  5599. * in to the sample max size
  5600. * data
  5601. * - user stack dump data
  5602. * dynamic size
  5603. * - the actual dumped size
  5604. */
  5605. /* Static size. */
  5606. perf_output_put(handle, dump_size);
  5607. /* Data. */
  5608. sp = perf_user_stack_pointer(regs);
  5609. rem = __output_copy_user(handle, (void *) sp, dump_size);
  5610. dyn_size = dump_size - rem;
  5611. perf_output_skip(handle, rem);
  5612. /* Dynamic size. */
  5613. perf_output_put(handle, dyn_size);
  5614. }
  5615. }
  5616. static unsigned long perf_prepare_sample_aux(struct perf_event *event,
  5617. struct perf_sample_data *data,
  5618. size_t size)
  5619. {
  5620. struct perf_event *sampler = event->aux_event;
  5621. struct perf_buffer *rb;
  5622. data->aux_size = 0;
  5623. if (!sampler)
  5624. goto out;
  5625. if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE))
  5626. goto out;
  5627. if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id()))
  5628. goto out;
  5629. rb = ring_buffer_get(sampler);
  5630. if (!rb)
  5631. goto out;
  5632. /*
  5633. * If this is an NMI hit inside sampling code, don't take
  5634. * the sample. See also perf_aux_sample_output().
  5635. */
  5636. if (READ_ONCE(rb->aux_in_sampling)) {
  5637. data->aux_size = 0;
  5638. } else {
  5639. size = min_t(size_t, size, perf_aux_size(rb));
  5640. data->aux_size = ALIGN(size, sizeof(u64));
  5641. }
  5642. ring_buffer_put(rb);
  5643. out:
  5644. return data->aux_size;
  5645. }
  5646. static long perf_pmu_snapshot_aux(struct perf_buffer *rb,
  5647. struct perf_event *event,
  5648. struct perf_output_handle *handle,
  5649. unsigned long size)
  5650. {
  5651. unsigned long flags;
  5652. long ret;
  5653. /*
  5654. * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler
  5655. * paths. If we start calling them in NMI context, they may race with
  5656. * the IRQ ones, that is, for example, re-starting an event that's just
  5657. * been stopped, which is why we're using a separate callback that
  5658. * doesn't change the event state.
  5659. *
  5660. * IRQs need to be disabled to prevent IPIs from racing with us.
  5661. */
  5662. local_irq_save(flags);
  5663. /*
  5664. * Guard against NMI hits inside the critical section;
  5665. * see also perf_prepare_sample_aux().
  5666. */
  5667. WRITE_ONCE(rb->aux_in_sampling, 1);
  5668. barrier();
  5669. ret = event->pmu->snapshot_aux(event, handle, size);
  5670. barrier();
  5671. WRITE_ONCE(rb->aux_in_sampling, 0);
  5672. local_irq_restore(flags);
  5673. return ret;
  5674. }
  5675. static void perf_aux_sample_output(struct perf_event *event,
  5676. struct perf_output_handle *handle,
  5677. struct perf_sample_data *data)
  5678. {
  5679. struct perf_event *sampler = event->aux_event;
  5680. struct perf_buffer *rb;
  5681. unsigned long pad;
  5682. long size;
  5683. if (WARN_ON_ONCE(!sampler || !data->aux_size))
  5684. return;
  5685. rb = ring_buffer_get(sampler);
  5686. if (!rb)
  5687. return;
  5688. size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size);
  5689. /*
  5690. * An error here means that perf_output_copy() failed (returned a
  5691. * non-zero surplus that it didn't copy), which in its current
  5692. * enlightened implementation is not possible. If that changes, we'd
  5693. * like to know.
  5694. */
  5695. if (WARN_ON_ONCE(size < 0))
  5696. goto out_put;
  5697. /*
  5698. * The pad comes from ALIGN()ing data->aux_size up to u64 in
  5699. * perf_prepare_sample_aux(), so should not be more than that.
  5700. */
  5701. pad = data->aux_size - size;
  5702. if (WARN_ON_ONCE(pad >= sizeof(u64)))
  5703. pad = 8;
  5704. if (pad) {
  5705. u64 zero = 0;
  5706. perf_output_copy(handle, &zero, pad);
  5707. }
  5708. out_put:
  5709. ring_buffer_put(rb);
  5710. }
  5711. static void __perf_event_header__init_id(struct perf_event_header *header,
  5712. struct perf_sample_data *data,
  5713. struct perf_event *event,
  5714. u64 sample_type)
  5715. {
  5716. data->type = event->attr.sample_type;
  5717. header->size += event->id_header_size;
  5718. if (sample_type & PERF_SAMPLE_TID) {
  5719. /* namespace issues */
  5720. data->tid_entry.pid = perf_event_pid(event, current);
  5721. data->tid_entry.tid = perf_event_tid(event, current);
  5722. }
  5723. if (sample_type & PERF_SAMPLE_TIME)
  5724. data->time = perf_event_clock(event);
  5725. if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
  5726. data->id = primary_event_id(event);
  5727. if (sample_type & PERF_SAMPLE_STREAM_ID)
  5728. data->stream_id = event->id;
  5729. if (sample_type & PERF_SAMPLE_CPU) {
  5730. data->cpu_entry.cpu = raw_smp_processor_id();
  5731. data->cpu_entry.reserved = 0;
  5732. }
  5733. }
  5734. void perf_event_header__init_id(struct perf_event_header *header,
  5735. struct perf_sample_data *data,
  5736. struct perf_event *event)
  5737. {
  5738. if (event->attr.sample_id_all)
  5739. __perf_event_header__init_id(header, data, event, event->attr.sample_type);
  5740. }
  5741. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  5742. struct perf_sample_data *data)
  5743. {
  5744. u64 sample_type = data->type;
  5745. if (sample_type & PERF_SAMPLE_TID)
  5746. perf_output_put(handle, data->tid_entry);
  5747. if (sample_type & PERF_SAMPLE_TIME)
  5748. perf_output_put(handle, data->time);
  5749. if (sample_type & PERF_SAMPLE_ID)
  5750. perf_output_put(handle, data->id);
  5751. if (sample_type & PERF_SAMPLE_STREAM_ID)
  5752. perf_output_put(handle, data->stream_id);
  5753. if (sample_type & PERF_SAMPLE_CPU)
  5754. perf_output_put(handle, data->cpu_entry);
  5755. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  5756. perf_output_put(handle, data->id);
  5757. }
  5758. void perf_event__output_id_sample(struct perf_event *event,
  5759. struct perf_output_handle *handle,
  5760. struct perf_sample_data *sample)
  5761. {
  5762. if (event->attr.sample_id_all)
  5763. __perf_event__output_id_sample(handle, sample);
  5764. }
  5765. static void perf_output_read_one(struct perf_output_handle *handle,
  5766. struct perf_event *event,
  5767. u64 enabled, u64 running)
  5768. {
  5769. u64 read_format = event->attr.read_format;
  5770. u64 values[5];
  5771. int n = 0;
  5772. values[n++] = perf_event_count(event);
  5773. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  5774. values[n++] = enabled +
  5775. atomic64_read(&event->child_total_time_enabled);
  5776. }
  5777. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  5778. values[n++] = running +
  5779. atomic64_read(&event->child_total_time_running);
  5780. }
  5781. if (read_format & PERF_FORMAT_ID)
  5782. values[n++] = primary_event_id(event);
  5783. if (read_format & PERF_FORMAT_LOST)
  5784. values[n++] = atomic64_read(&event->lost_samples);
  5785. __output_copy(handle, values, n * sizeof(u64));
  5786. }
  5787. static void perf_output_read_group(struct perf_output_handle *handle,
  5788. struct perf_event *event,
  5789. u64 enabled, u64 running)
  5790. {
  5791. struct perf_event *leader = event->group_leader, *sub;
  5792. u64 read_format = event->attr.read_format;
  5793. unsigned long flags;
  5794. u64 values[6];
  5795. int n = 0;
  5796. /*
  5797. * Disabling interrupts avoids all counter scheduling
  5798. * (context switches, timer based rotation and IPIs).
  5799. */
  5800. local_irq_save(flags);
  5801. values[n++] = 1 + leader->nr_siblings;
  5802. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  5803. values[n++] = enabled;
  5804. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  5805. values[n++] = running;
  5806. if ((leader != event) &&
  5807. (leader->state == PERF_EVENT_STATE_ACTIVE))
  5808. leader->pmu->read(leader);
  5809. values[n++] = perf_event_count(leader);
  5810. if (read_format & PERF_FORMAT_ID)
  5811. values[n++] = primary_event_id(leader);
  5812. if (read_format & PERF_FORMAT_LOST)
  5813. values[n++] = atomic64_read(&leader->lost_samples);
  5814. __output_copy(handle, values, n * sizeof(u64));
  5815. for_each_sibling_event(sub, leader) {
  5816. n = 0;
  5817. if ((sub != event) &&
  5818. (sub->state == PERF_EVENT_STATE_ACTIVE))
  5819. sub->pmu->read(sub);
  5820. values[n++] = perf_event_count(sub);
  5821. if (read_format & PERF_FORMAT_ID)
  5822. values[n++] = primary_event_id(sub);
  5823. if (read_format & PERF_FORMAT_LOST)
  5824. values[n++] = atomic64_read(&sub->lost_samples);
  5825. __output_copy(handle, values, n * sizeof(u64));
  5826. }
  5827. local_irq_restore(flags);
  5828. }
  5829. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  5830. PERF_FORMAT_TOTAL_TIME_RUNNING)
  5831. /*
  5832. * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
  5833. *
  5834. * The problem is that its both hard and excessively expensive to iterate the
  5835. * child list, not to mention that its impossible to IPI the children running
  5836. * on another CPU, from interrupt/NMI context.
  5837. */
  5838. static void perf_output_read(struct perf_output_handle *handle,
  5839. struct perf_event *event)
  5840. {
  5841. u64 enabled = 0, running = 0, now;
  5842. u64 read_format = event->attr.read_format;
  5843. /*
  5844. * compute total_time_enabled, total_time_running
  5845. * based on snapshot values taken when the event
  5846. * was last scheduled in.
  5847. *
  5848. * we cannot simply called update_context_time()
  5849. * because of locking issue as we are called in
  5850. * NMI context
  5851. */
  5852. if (read_format & PERF_FORMAT_TOTAL_TIMES)
  5853. calc_timer_values(event, &now, &enabled, &running);
  5854. if (event->attr.read_format & PERF_FORMAT_GROUP)
  5855. perf_output_read_group(handle, event, enabled, running);
  5856. else
  5857. perf_output_read_one(handle, event, enabled, running);
  5858. }
  5859. void perf_output_sample(struct perf_output_handle *handle,
  5860. struct perf_event_header *header,
  5861. struct perf_sample_data *data,
  5862. struct perf_event *event)
  5863. {
  5864. u64 sample_type = data->type;
  5865. perf_output_put(handle, *header);
  5866. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  5867. perf_output_put(handle, data->id);
  5868. if (sample_type & PERF_SAMPLE_IP)
  5869. perf_output_put(handle, data->ip);
  5870. if (sample_type & PERF_SAMPLE_TID)
  5871. perf_output_put(handle, data->tid_entry);
  5872. if (sample_type & PERF_SAMPLE_TIME)
  5873. perf_output_put(handle, data->time);
  5874. if (sample_type & PERF_SAMPLE_ADDR)
  5875. perf_output_put(handle, data->addr);
  5876. if (sample_type & PERF_SAMPLE_ID)
  5877. perf_output_put(handle, data->id);
  5878. if (sample_type & PERF_SAMPLE_STREAM_ID)
  5879. perf_output_put(handle, data->stream_id);
  5880. if (sample_type & PERF_SAMPLE_CPU)
  5881. perf_output_put(handle, data->cpu_entry);
  5882. if (sample_type & PERF_SAMPLE_PERIOD)
  5883. perf_output_put(handle, data->period);
  5884. if (sample_type & PERF_SAMPLE_READ)
  5885. perf_output_read(handle, event);
  5886. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  5887. int size = 1;
  5888. size += data->callchain->nr;
  5889. size *= sizeof(u64);
  5890. __output_copy(handle, data->callchain, size);
  5891. }
  5892. if (sample_type & PERF_SAMPLE_RAW) {
  5893. struct perf_raw_record *raw = data->raw;
  5894. if (raw) {
  5895. struct perf_raw_frag *frag = &raw->frag;
  5896. perf_output_put(handle, raw->size);
  5897. do {
  5898. if (frag->copy) {
  5899. __output_custom(handle, frag->copy,
  5900. frag->data, frag->size);
  5901. } else {
  5902. __output_copy(handle, frag->data,
  5903. frag->size);
  5904. }
  5905. if (perf_raw_frag_last(frag))
  5906. break;
  5907. frag = frag->next;
  5908. } while (1);
  5909. if (frag->pad)
  5910. __output_skip(handle, NULL, frag->pad);
  5911. } else {
  5912. struct {
  5913. u32 size;
  5914. u32 data;
  5915. } raw = {
  5916. .size = sizeof(u32),
  5917. .data = 0,
  5918. };
  5919. perf_output_put(handle, raw);
  5920. }
  5921. }
  5922. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  5923. if (data->sample_flags & PERF_SAMPLE_BRANCH_STACK) {
  5924. size_t size;
  5925. size = data->br_stack->nr
  5926. * sizeof(struct perf_branch_entry);
  5927. perf_output_put(handle, data->br_stack->nr);
  5928. if (branch_sample_hw_index(event))
  5929. perf_output_put(handle, data->br_stack->hw_idx);
  5930. perf_output_copy(handle, data->br_stack->entries, size);
  5931. } else {
  5932. /*
  5933. * we always store at least the value of nr
  5934. */
  5935. u64 nr = 0;
  5936. perf_output_put(handle, nr);
  5937. }
  5938. }
  5939. if (sample_type & PERF_SAMPLE_REGS_USER) {
  5940. u64 abi = data->regs_user.abi;
  5941. /*
  5942. * If there are no regs to dump, notice it through
  5943. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  5944. */
  5945. perf_output_put(handle, abi);
  5946. if (abi) {
  5947. u64 mask = event->attr.sample_regs_user;
  5948. perf_output_sample_regs(handle,
  5949. data->regs_user.regs,
  5950. mask);
  5951. }
  5952. }
  5953. if (sample_type & PERF_SAMPLE_STACK_USER) {
  5954. perf_output_sample_ustack(handle,
  5955. data->stack_user_size,
  5956. data->regs_user.regs);
  5957. }
  5958. if (sample_type & PERF_SAMPLE_WEIGHT_TYPE)
  5959. perf_output_put(handle, data->weight.full);
  5960. if (sample_type & PERF_SAMPLE_DATA_SRC)
  5961. perf_output_put(handle, data->data_src.val);
  5962. if (sample_type & PERF_SAMPLE_TRANSACTION)
  5963. perf_output_put(handle, data->txn);
  5964. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  5965. u64 abi = data->regs_intr.abi;
  5966. /*
  5967. * If there are no regs to dump, notice it through
  5968. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  5969. */
  5970. perf_output_put(handle, abi);
  5971. if (abi) {
  5972. u64 mask = event->attr.sample_regs_intr;
  5973. perf_output_sample_regs(handle,
  5974. data->regs_intr.regs,
  5975. mask);
  5976. }
  5977. }
  5978. if (sample_type & PERF_SAMPLE_PHYS_ADDR)
  5979. perf_output_put(handle, data->phys_addr);
  5980. if (sample_type & PERF_SAMPLE_CGROUP)
  5981. perf_output_put(handle, data->cgroup);
  5982. if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)
  5983. perf_output_put(handle, data->data_page_size);
  5984. if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)
  5985. perf_output_put(handle, data->code_page_size);
  5986. if (sample_type & PERF_SAMPLE_AUX) {
  5987. perf_output_put(handle, data->aux_size);
  5988. if (data->aux_size)
  5989. perf_aux_sample_output(event, handle, data);
  5990. }
  5991. if (!event->attr.watermark) {
  5992. int wakeup_events = event->attr.wakeup_events;
  5993. if (wakeup_events) {
  5994. struct perf_buffer *rb = handle->rb;
  5995. int events = local_inc_return(&rb->events);
  5996. if (events >= wakeup_events) {
  5997. local_sub(wakeup_events, &rb->events);
  5998. local_inc(&rb->wakeup);
  5999. }
  6000. }
  6001. }
  6002. }
  6003. static u64 perf_virt_to_phys(u64 virt)
  6004. {
  6005. u64 phys_addr = 0;
  6006. if (!virt)
  6007. return 0;
  6008. if (virt >= TASK_SIZE) {
  6009. /* If it's vmalloc()d memory, leave phys_addr as 0 */
  6010. if (virt_addr_valid((void *)(uintptr_t)virt) &&
  6011. !(virt >= VMALLOC_START && virt < VMALLOC_END))
  6012. phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt);
  6013. } else {
  6014. /*
  6015. * Walking the pages tables for user address.
  6016. * Interrupts are disabled, so it prevents any tear down
  6017. * of the page tables.
  6018. * Try IRQ-safe get_user_page_fast_only first.
  6019. * If failed, leave phys_addr as 0.
  6020. */
  6021. if (current->mm != NULL) {
  6022. struct page *p;
  6023. pagefault_disable();
  6024. if (get_user_page_fast_only(virt, 0, &p)) {
  6025. phys_addr = page_to_phys(p) + virt % PAGE_SIZE;
  6026. put_page(p);
  6027. }
  6028. pagefault_enable();
  6029. }
  6030. }
  6031. return phys_addr;
  6032. }
  6033. /*
  6034. * Return the pagetable size of a given virtual address.
  6035. */
  6036. static u64 perf_get_pgtable_size(struct mm_struct *mm, unsigned long addr)
  6037. {
  6038. u64 size = 0;
  6039. #ifdef CONFIG_HAVE_FAST_GUP
  6040. pgd_t *pgdp, pgd;
  6041. p4d_t *p4dp, p4d;
  6042. pud_t *pudp, pud;
  6043. pmd_t *pmdp, pmd;
  6044. pte_t *ptep, pte;
  6045. pgdp = pgd_offset(mm, addr);
  6046. pgd = READ_ONCE(*pgdp);
  6047. if (pgd_none(pgd))
  6048. return 0;
  6049. if (pgd_leaf(pgd))
  6050. return pgd_leaf_size(pgd);
  6051. p4dp = p4d_offset_lockless(pgdp, pgd, addr);
  6052. p4d = READ_ONCE(*p4dp);
  6053. if (!p4d_present(p4d))
  6054. return 0;
  6055. if (p4d_leaf(p4d))
  6056. return p4d_leaf_size(p4d);
  6057. pudp = pud_offset_lockless(p4dp, p4d, addr);
  6058. pud = READ_ONCE(*pudp);
  6059. if (!pud_present(pud))
  6060. return 0;
  6061. if (pud_leaf(pud))
  6062. return pud_leaf_size(pud);
  6063. pmdp = pmd_offset_lockless(pudp, pud, addr);
  6064. pmd = READ_ONCE(*pmdp);
  6065. if (!pmd_present(pmd))
  6066. return 0;
  6067. if (pmd_leaf(pmd))
  6068. return pmd_leaf_size(pmd);
  6069. ptep = pte_offset_map(&pmd, addr);
  6070. pte = ptep_get_lockless(ptep);
  6071. if (pte_present(pte))
  6072. size = pte_leaf_size(pte);
  6073. pte_unmap(ptep);
  6074. #endif /* CONFIG_HAVE_FAST_GUP */
  6075. return size;
  6076. }
  6077. static u64 perf_get_page_size(unsigned long addr)
  6078. {
  6079. struct mm_struct *mm;
  6080. unsigned long flags;
  6081. u64 size;
  6082. if (!addr)
  6083. return 0;
  6084. /*
  6085. * Software page-table walkers must disable IRQs,
  6086. * which prevents any tear down of the page tables.
  6087. */
  6088. local_irq_save(flags);
  6089. mm = current->mm;
  6090. if (!mm) {
  6091. /*
  6092. * For kernel threads and the like, use init_mm so that
  6093. * we can find kernel memory.
  6094. */
  6095. mm = &init_mm;
  6096. }
  6097. size = perf_get_pgtable_size(mm, addr);
  6098. local_irq_restore(flags);
  6099. return size;
  6100. }
  6101. static struct perf_callchain_entry __empty_callchain = { .nr = 0, };
  6102. struct perf_callchain_entry *
  6103. perf_callchain(struct perf_event *event, struct pt_regs *regs)
  6104. {
  6105. bool kernel = !event->attr.exclude_callchain_kernel;
  6106. bool user = !event->attr.exclude_callchain_user;
  6107. /* Disallow cross-task user callchains. */
  6108. bool crosstask = event->ctx->task && event->ctx->task != current;
  6109. const u32 max_stack = event->attr.sample_max_stack;
  6110. struct perf_callchain_entry *callchain;
  6111. if (!kernel && !user)
  6112. return &__empty_callchain;
  6113. callchain = get_perf_callchain(regs, 0, kernel, user,
  6114. max_stack, crosstask, true);
  6115. return callchain ?: &__empty_callchain;
  6116. }
  6117. void perf_prepare_sample(struct perf_event_header *header,
  6118. struct perf_sample_data *data,
  6119. struct perf_event *event,
  6120. struct pt_regs *regs)
  6121. {
  6122. u64 sample_type = event->attr.sample_type;
  6123. u64 filtered_sample_type;
  6124. header->type = PERF_RECORD_SAMPLE;
  6125. header->size = sizeof(*header) + event->header_size;
  6126. header->misc = 0;
  6127. header->misc |= perf_misc_flags(regs);
  6128. /*
  6129. * Clear the sample flags that have already been done by the
  6130. * PMU driver.
  6131. */
  6132. filtered_sample_type = sample_type & ~data->sample_flags;
  6133. __perf_event_header__init_id(header, data, event, filtered_sample_type);
  6134. if (sample_type & (PERF_SAMPLE_IP | PERF_SAMPLE_CODE_PAGE_SIZE))
  6135. data->ip = perf_instruction_pointer(regs);
  6136. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  6137. int size = 1;
  6138. if (filtered_sample_type & PERF_SAMPLE_CALLCHAIN)
  6139. data->callchain = perf_callchain(event, regs);
  6140. size += data->callchain->nr;
  6141. header->size += size * sizeof(u64);
  6142. }
  6143. if (sample_type & PERF_SAMPLE_RAW) {
  6144. struct perf_raw_record *raw = data->raw;
  6145. int size;
  6146. if (raw && (data->sample_flags & PERF_SAMPLE_RAW)) {
  6147. struct perf_raw_frag *frag = &raw->frag;
  6148. u32 sum = 0;
  6149. do {
  6150. sum += frag->size;
  6151. if (perf_raw_frag_last(frag))
  6152. break;
  6153. frag = frag->next;
  6154. } while (1);
  6155. size = round_up(sum + sizeof(u32), sizeof(u64));
  6156. raw->size = size - sizeof(u32);
  6157. frag->pad = raw->size - sum;
  6158. } else {
  6159. size = sizeof(u64);
  6160. data->raw = NULL;
  6161. }
  6162. header->size += size;
  6163. }
  6164. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  6165. int size = sizeof(u64); /* nr */
  6166. if (data->sample_flags & PERF_SAMPLE_BRANCH_STACK) {
  6167. if (branch_sample_hw_index(event))
  6168. size += sizeof(u64);
  6169. size += data->br_stack->nr
  6170. * sizeof(struct perf_branch_entry);
  6171. }
  6172. header->size += size;
  6173. }
  6174. if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
  6175. perf_sample_regs_user(&data->regs_user, regs);
  6176. if (sample_type & PERF_SAMPLE_REGS_USER) {
  6177. /* regs dump ABI info */
  6178. int size = sizeof(u64);
  6179. if (data->regs_user.regs) {
  6180. u64 mask = event->attr.sample_regs_user;
  6181. size += hweight64(mask) * sizeof(u64);
  6182. }
  6183. header->size += size;
  6184. }
  6185. if (sample_type & PERF_SAMPLE_STACK_USER) {
  6186. /*
  6187. * Either we need PERF_SAMPLE_STACK_USER bit to be always
  6188. * processed as the last one or have additional check added
  6189. * in case new sample type is added, because we could eat
  6190. * up the rest of the sample size.
  6191. */
  6192. u16 stack_size = event->attr.sample_stack_user;
  6193. u16 size = sizeof(u64);
  6194. stack_size = perf_sample_ustack_size(stack_size, header->size,
  6195. data->regs_user.regs);
  6196. /*
  6197. * If there is something to dump, add space for the dump
  6198. * itself and for the field that tells the dynamic size,
  6199. * which is how many have been actually dumped.
  6200. */
  6201. if (stack_size)
  6202. size += sizeof(u64) + stack_size;
  6203. data->stack_user_size = stack_size;
  6204. header->size += size;
  6205. }
  6206. if (filtered_sample_type & PERF_SAMPLE_WEIGHT_TYPE)
  6207. data->weight.full = 0;
  6208. if (filtered_sample_type & PERF_SAMPLE_DATA_SRC)
  6209. data->data_src.val = PERF_MEM_NA;
  6210. if (filtered_sample_type & PERF_SAMPLE_TRANSACTION)
  6211. data->txn = 0;
  6212. if (sample_type & (PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR | PERF_SAMPLE_DATA_PAGE_SIZE)) {
  6213. if (filtered_sample_type & PERF_SAMPLE_ADDR)
  6214. data->addr = 0;
  6215. }
  6216. if (sample_type & PERF_SAMPLE_REGS_INTR) {
  6217. /* regs dump ABI info */
  6218. int size = sizeof(u64);
  6219. perf_sample_regs_intr(&data->regs_intr, regs);
  6220. if (data->regs_intr.regs) {
  6221. u64 mask = event->attr.sample_regs_intr;
  6222. size += hweight64(mask) * sizeof(u64);
  6223. }
  6224. header->size += size;
  6225. }
  6226. if (sample_type & PERF_SAMPLE_PHYS_ADDR &&
  6227. filtered_sample_type & PERF_SAMPLE_PHYS_ADDR)
  6228. data->phys_addr = perf_virt_to_phys(data->addr);
  6229. #ifdef CONFIG_CGROUP_PERF
  6230. if (sample_type & PERF_SAMPLE_CGROUP) {
  6231. struct cgroup *cgrp;
  6232. /* protected by RCU */
  6233. cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup;
  6234. data->cgroup = cgroup_id(cgrp);
  6235. }
  6236. #endif
  6237. /*
  6238. * PERF_DATA_PAGE_SIZE requires PERF_SAMPLE_ADDR. If the user doesn't
  6239. * require PERF_SAMPLE_ADDR, kernel implicitly retrieve the data->addr,
  6240. * but the value will not dump to the userspace.
  6241. */
  6242. if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)
  6243. data->data_page_size = perf_get_page_size(data->addr);
  6244. if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)
  6245. data->code_page_size = perf_get_page_size(data->ip);
  6246. if (sample_type & PERF_SAMPLE_AUX) {
  6247. u64 size;
  6248. header->size += sizeof(u64); /* size */
  6249. /*
  6250. * Given the 16bit nature of header::size, an AUX sample can
  6251. * easily overflow it, what with all the preceding sample bits.
  6252. * Make sure this doesn't happen by using up to U16_MAX bytes
  6253. * per sample in total (rounded down to 8 byte boundary).
  6254. */
  6255. size = min_t(size_t, U16_MAX - header->size,
  6256. event->attr.aux_sample_size);
  6257. size = rounddown(size, 8);
  6258. size = perf_prepare_sample_aux(event, data, size);
  6259. WARN_ON_ONCE(size + header->size > U16_MAX);
  6260. header->size += size;
  6261. }
  6262. /*
  6263. * If you're adding more sample types here, you likely need to do
  6264. * something about the overflowing header::size, like repurpose the
  6265. * lowest 3 bits of size, which should be always zero at the moment.
  6266. * This raises a more important question, do we really need 512k sized
  6267. * samples and why, so good argumentation is in order for whatever you
  6268. * do here next.
  6269. */
  6270. WARN_ON_ONCE(header->size & 7);
  6271. }
  6272. static __always_inline int
  6273. __perf_event_output(struct perf_event *event,
  6274. struct perf_sample_data *data,
  6275. struct pt_regs *regs,
  6276. int (*output_begin)(struct perf_output_handle *,
  6277. struct perf_sample_data *,
  6278. struct perf_event *,
  6279. unsigned int))
  6280. {
  6281. struct perf_output_handle handle;
  6282. struct perf_event_header header;
  6283. int err;
  6284. /* protect the callchain buffers */
  6285. rcu_read_lock();
  6286. perf_prepare_sample(&header, data, event, regs);
  6287. err = output_begin(&handle, data, event, header.size);
  6288. if (err)
  6289. goto exit;
  6290. perf_output_sample(&handle, &header, data, event);
  6291. perf_output_end(&handle);
  6292. exit:
  6293. rcu_read_unlock();
  6294. return err;
  6295. }
  6296. void
  6297. perf_event_output_forward(struct perf_event *event,
  6298. struct perf_sample_data *data,
  6299. struct pt_regs *regs)
  6300. {
  6301. __perf_event_output(event, data, regs, perf_output_begin_forward);
  6302. }
  6303. void
  6304. perf_event_output_backward(struct perf_event *event,
  6305. struct perf_sample_data *data,
  6306. struct pt_regs *regs)
  6307. {
  6308. __perf_event_output(event, data, regs, perf_output_begin_backward);
  6309. }
  6310. int
  6311. perf_event_output(struct perf_event *event,
  6312. struct perf_sample_data *data,
  6313. struct pt_regs *regs)
  6314. {
  6315. return __perf_event_output(event, data, regs, perf_output_begin);
  6316. }
  6317. /*
  6318. * read event_id
  6319. */
  6320. struct perf_read_event {
  6321. struct perf_event_header header;
  6322. u32 pid;
  6323. u32 tid;
  6324. };
  6325. static void
  6326. perf_event_read_event(struct perf_event *event,
  6327. struct task_struct *task)
  6328. {
  6329. struct perf_output_handle handle;
  6330. struct perf_sample_data sample;
  6331. struct perf_read_event read_event = {
  6332. .header = {
  6333. .type = PERF_RECORD_READ,
  6334. .misc = 0,
  6335. .size = sizeof(read_event) + event->read_size,
  6336. },
  6337. .pid = perf_event_pid(event, task),
  6338. .tid = perf_event_tid(event, task),
  6339. };
  6340. int ret;
  6341. perf_event_header__init_id(&read_event.header, &sample, event);
  6342. ret = perf_output_begin(&handle, &sample, event, read_event.header.size);
  6343. if (ret)
  6344. return;
  6345. perf_output_put(&handle, read_event);
  6346. perf_output_read(&handle, event);
  6347. perf_event__output_id_sample(event, &handle, &sample);
  6348. perf_output_end(&handle);
  6349. }
  6350. typedef void (perf_iterate_f)(struct perf_event *event, void *data);
  6351. static void
  6352. perf_iterate_ctx(struct perf_event_context *ctx,
  6353. perf_iterate_f output,
  6354. void *data, bool all)
  6355. {
  6356. struct perf_event *event;
  6357. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  6358. if (!all) {
  6359. if (event->state < PERF_EVENT_STATE_INACTIVE)
  6360. continue;
  6361. if (!event_filter_match(event))
  6362. continue;
  6363. }
  6364. output(event, data);
  6365. }
  6366. }
  6367. static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
  6368. {
  6369. struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
  6370. struct perf_event *event;
  6371. list_for_each_entry_rcu(event, &pel->list, sb_list) {
  6372. /*
  6373. * Skip events that are not fully formed yet; ensure that
  6374. * if we observe event->ctx, both event and ctx will be
  6375. * complete enough. See perf_install_in_context().
  6376. */
  6377. if (!smp_load_acquire(&event->ctx))
  6378. continue;
  6379. if (event->state < PERF_EVENT_STATE_INACTIVE)
  6380. continue;
  6381. if (!event_filter_match(event))
  6382. continue;
  6383. output(event, data);
  6384. }
  6385. }
  6386. /*
  6387. * Iterate all events that need to receive side-band events.
  6388. *
  6389. * For new callers; ensure that account_pmu_sb_event() includes
  6390. * your event, otherwise it might not get delivered.
  6391. */
  6392. static void
  6393. perf_iterate_sb(perf_iterate_f output, void *data,
  6394. struct perf_event_context *task_ctx)
  6395. {
  6396. struct perf_event_context *ctx;
  6397. int ctxn;
  6398. rcu_read_lock();
  6399. preempt_disable();
  6400. /*
  6401. * If we have task_ctx != NULL we only notify the task context itself.
  6402. * The task_ctx is set only for EXIT events before releasing task
  6403. * context.
  6404. */
  6405. if (task_ctx) {
  6406. perf_iterate_ctx(task_ctx, output, data, false);
  6407. goto done;
  6408. }
  6409. perf_iterate_sb_cpu(output, data);
  6410. for_each_task_context_nr(ctxn) {
  6411. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  6412. if (ctx)
  6413. perf_iterate_ctx(ctx, output, data, false);
  6414. }
  6415. done:
  6416. preempt_enable();
  6417. rcu_read_unlock();
  6418. }
  6419. /*
  6420. * Clear all file-based filters at exec, they'll have to be
  6421. * re-instated when/if these objects are mmapped again.
  6422. */
  6423. static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
  6424. {
  6425. struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
  6426. struct perf_addr_filter *filter;
  6427. unsigned int restart = 0, count = 0;
  6428. unsigned long flags;
  6429. if (!has_addr_filter(event))
  6430. return;
  6431. raw_spin_lock_irqsave(&ifh->lock, flags);
  6432. list_for_each_entry(filter, &ifh->list, entry) {
  6433. if (filter->path.dentry) {
  6434. event->addr_filter_ranges[count].start = 0;
  6435. event->addr_filter_ranges[count].size = 0;
  6436. restart++;
  6437. }
  6438. count++;
  6439. }
  6440. if (restart)
  6441. event->addr_filters_gen++;
  6442. raw_spin_unlock_irqrestore(&ifh->lock, flags);
  6443. if (restart)
  6444. perf_event_stop(event, 1);
  6445. }
  6446. void perf_event_exec(void)
  6447. {
  6448. struct perf_event_context *ctx;
  6449. int ctxn;
  6450. for_each_task_context_nr(ctxn) {
  6451. perf_event_enable_on_exec(ctxn);
  6452. perf_event_remove_on_exec(ctxn);
  6453. rcu_read_lock();
  6454. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  6455. if (ctx) {
  6456. perf_iterate_ctx(ctx, perf_event_addr_filters_exec,
  6457. NULL, true);
  6458. }
  6459. rcu_read_unlock();
  6460. }
  6461. }
  6462. struct remote_output {
  6463. struct perf_buffer *rb;
  6464. int err;
  6465. };
  6466. static void __perf_event_output_stop(struct perf_event *event, void *data)
  6467. {
  6468. struct perf_event *parent = event->parent;
  6469. struct remote_output *ro = data;
  6470. struct perf_buffer *rb = ro->rb;
  6471. struct stop_event_data sd = {
  6472. .event = event,
  6473. };
  6474. if (!has_aux(event))
  6475. return;
  6476. if (!parent)
  6477. parent = event;
  6478. /*
  6479. * In case of inheritance, it will be the parent that links to the
  6480. * ring-buffer, but it will be the child that's actually using it.
  6481. *
  6482. * We are using event::rb to determine if the event should be stopped,
  6483. * however this may race with ring_buffer_attach() (through set_output),
  6484. * which will make us skip the event that actually needs to be stopped.
  6485. * So ring_buffer_attach() has to stop an aux event before re-assigning
  6486. * its rb pointer.
  6487. */
  6488. if (rcu_dereference(parent->rb) == rb)
  6489. ro->err = __perf_event_stop(&sd);
  6490. }
  6491. static int __perf_pmu_output_stop(void *info)
  6492. {
  6493. struct perf_event *event = info;
  6494. struct pmu *pmu = event->ctx->pmu;
  6495. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  6496. struct remote_output ro = {
  6497. .rb = event->rb,
  6498. };
  6499. rcu_read_lock();
  6500. perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
  6501. if (cpuctx->task_ctx)
  6502. perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
  6503. &ro, false);
  6504. rcu_read_unlock();
  6505. return ro.err;
  6506. }
  6507. static void perf_pmu_output_stop(struct perf_event *event)
  6508. {
  6509. struct perf_event *iter;
  6510. int err, cpu;
  6511. restart:
  6512. rcu_read_lock();
  6513. list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
  6514. /*
  6515. * For per-CPU events, we need to make sure that neither they
  6516. * nor their children are running; for cpu==-1 events it's
  6517. * sufficient to stop the event itself if it's active, since
  6518. * it can't have children.
  6519. */
  6520. cpu = iter->cpu;
  6521. if (cpu == -1)
  6522. cpu = READ_ONCE(iter->oncpu);
  6523. if (cpu == -1)
  6524. continue;
  6525. err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
  6526. if (err == -EAGAIN) {
  6527. rcu_read_unlock();
  6528. goto restart;
  6529. }
  6530. }
  6531. rcu_read_unlock();
  6532. }
  6533. /*
  6534. * task tracking -- fork/exit
  6535. *
  6536. * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
  6537. */
  6538. struct perf_task_event {
  6539. struct task_struct *task;
  6540. struct perf_event_context *task_ctx;
  6541. struct {
  6542. struct perf_event_header header;
  6543. u32 pid;
  6544. u32 ppid;
  6545. u32 tid;
  6546. u32 ptid;
  6547. u64 time;
  6548. } event_id;
  6549. };
  6550. static int perf_event_task_match(struct perf_event *event)
  6551. {
  6552. return event->attr.comm || event->attr.mmap ||
  6553. event->attr.mmap2 || event->attr.mmap_data ||
  6554. event->attr.task;
  6555. }
  6556. static void perf_event_task_output(struct perf_event *event,
  6557. void *data)
  6558. {
  6559. struct perf_task_event *task_event = data;
  6560. struct perf_output_handle handle;
  6561. struct perf_sample_data sample;
  6562. struct task_struct *task = task_event->task;
  6563. int ret, size = task_event->event_id.header.size;
  6564. if (!perf_event_task_match(event))
  6565. return;
  6566. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  6567. ret = perf_output_begin(&handle, &sample, event,
  6568. task_event->event_id.header.size);
  6569. if (ret)
  6570. goto out;
  6571. task_event->event_id.pid = perf_event_pid(event, task);
  6572. task_event->event_id.tid = perf_event_tid(event, task);
  6573. if (task_event->event_id.header.type == PERF_RECORD_EXIT) {
  6574. task_event->event_id.ppid = perf_event_pid(event,
  6575. task->real_parent);
  6576. task_event->event_id.ptid = perf_event_pid(event,
  6577. task->real_parent);
  6578. } else { /* PERF_RECORD_FORK */
  6579. task_event->event_id.ppid = perf_event_pid(event, current);
  6580. task_event->event_id.ptid = perf_event_tid(event, current);
  6581. }
  6582. task_event->event_id.time = perf_event_clock(event);
  6583. perf_output_put(&handle, task_event->event_id);
  6584. perf_event__output_id_sample(event, &handle, &sample);
  6585. perf_output_end(&handle);
  6586. out:
  6587. task_event->event_id.header.size = size;
  6588. }
  6589. static void perf_event_task(struct task_struct *task,
  6590. struct perf_event_context *task_ctx,
  6591. int new)
  6592. {
  6593. struct perf_task_event task_event;
  6594. if (!atomic_read(&nr_comm_events) &&
  6595. !atomic_read(&nr_mmap_events) &&
  6596. !atomic_read(&nr_task_events))
  6597. return;
  6598. task_event = (struct perf_task_event){
  6599. .task = task,
  6600. .task_ctx = task_ctx,
  6601. .event_id = {
  6602. .header = {
  6603. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  6604. .misc = 0,
  6605. .size = sizeof(task_event.event_id),
  6606. },
  6607. /* .pid */
  6608. /* .ppid */
  6609. /* .tid */
  6610. /* .ptid */
  6611. /* .time */
  6612. },
  6613. };
  6614. perf_iterate_sb(perf_event_task_output,
  6615. &task_event,
  6616. task_ctx);
  6617. }
  6618. void perf_event_fork(struct task_struct *task)
  6619. {
  6620. perf_event_task(task, NULL, 1);
  6621. perf_event_namespaces(task);
  6622. }
  6623. /*
  6624. * comm tracking
  6625. */
  6626. struct perf_comm_event {
  6627. struct task_struct *task;
  6628. char *comm;
  6629. int comm_size;
  6630. struct {
  6631. struct perf_event_header header;
  6632. u32 pid;
  6633. u32 tid;
  6634. } event_id;
  6635. };
  6636. static int perf_event_comm_match(struct perf_event *event)
  6637. {
  6638. return event->attr.comm;
  6639. }
  6640. static void perf_event_comm_output(struct perf_event *event,
  6641. void *data)
  6642. {
  6643. struct perf_comm_event *comm_event = data;
  6644. struct perf_output_handle handle;
  6645. struct perf_sample_data sample;
  6646. int size = comm_event->event_id.header.size;
  6647. int ret;
  6648. if (!perf_event_comm_match(event))
  6649. return;
  6650. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  6651. ret = perf_output_begin(&handle, &sample, event,
  6652. comm_event->event_id.header.size);
  6653. if (ret)
  6654. goto out;
  6655. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  6656. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  6657. perf_output_put(&handle, comm_event->event_id);
  6658. __output_copy(&handle, comm_event->comm,
  6659. comm_event->comm_size);
  6660. perf_event__output_id_sample(event, &handle, &sample);
  6661. perf_output_end(&handle);
  6662. out:
  6663. comm_event->event_id.header.size = size;
  6664. }
  6665. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  6666. {
  6667. char comm[TASK_COMM_LEN];
  6668. unsigned int size;
  6669. memset(comm, 0, sizeof(comm));
  6670. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  6671. size = ALIGN(strlen(comm)+1, sizeof(u64));
  6672. comm_event->comm = comm;
  6673. comm_event->comm_size = size;
  6674. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  6675. perf_iterate_sb(perf_event_comm_output,
  6676. comm_event,
  6677. NULL);
  6678. }
  6679. void perf_event_comm(struct task_struct *task, bool exec)
  6680. {
  6681. struct perf_comm_event comm_event;
  6682. if (!atomic_read(&nr_comm_events))
  6683. return;
  6684. comm_event = (struct perf_comm_event){
  6685. .task = task,
  6686. /* .comm */
  6687. /* .comm_size */
  6688. .event_id = {
  6689. .header = {
  6690. .type = PERF_RECORD_COMM,
  6691. .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
  6692. /* .size */
  6693. },
  6694. /* .pid */
  6695. /* .tid */
  6696. },
  6697. };
  6698. perf_event_comm_event(&comm_event);
  6699. }
  6700. /*
  6701. * namespaces tracking
  6702. */
  6703. struct perf_namespaces_event {
  6704. struct task_struct *task;
  6705. struct {
  6706. struct perf_event_header header;
  6707. u32 pid;
  6708. u32 tid;
  6709. u64 nr_namespaces;
  6710. struct perf_ns_link_info link_info[NR_NAMESPACES];
  6711. } event_id;
  6712. };
  6713. static int perf_event_namespaces_match(struct perf_event *event)
  6714. {
  6715. return event->attr.namespaces;
  6716. }
  6717. static void perf_event_namespaces_output(struct perf_event *event,
  6718. void *data)
  6719. {
  6720. struct perf_namespaces_event *namespaces_event = data;
  6721. struct perf_output_handle handle;
  6722. struct perf_sample_data sample;
  6723. u16 header_size = namespaces_event->event_id.header.size;
  6724. int ret;
  6725. if (!perf_event_namespaces_match(event))
  6726. return;
  6727. perf_event_header__init_id(&namespaces_event->event_id.header,
  6728. &sample, event);
  6729. ret = perf_output_begin(&handle, &sample, event,
  6730. namespaces_event->event_id.header.size);
  6731. if (ret)
  6732. goto out;
  6733. namespaces_event->event_id.pid = perf_event_pid(event,
  6734. namespaces_event->task);
  6735. namespaces_event->event_id.tid = perf_event_tid(event,
  6736. namespaces_event->task);
  6737. perf_output_put(&handle, namespaces_event->event_id);
  6738. perf_event__output_id_sample(event, &handle, &sample);
  6739. perf_output_end(&handle);
  6740. out:
  6741. namespaces_event->event_id.header.size = header_size;
  6742. }
  6743. static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info,
  6744. struct task_struct *task,
  6745. const struct proc_ns_operations *ns_ops)
  6746. {
  6747. struct path ns_path;
  6748. struct inode *ns_inode;
  6749. int error;
  6750. error = ns_get_path(&ns_path, task, ns_ops);
  6751. if (!error) {
  6752. ns_inode = ns_path.dentry->d_inode;
  6753. ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev);
  6754. ns_link_info->ino = ns_inode->i_ino;
  6755. path_put(&ns_path);
  6756. }
  6757. }
  6758. void perf_event_namespaces(struct task_struct *task)
  6759. {
  6760. struct perf_namespaces_event namespaces_event;
  6761. struct perf_ns_link_info *ns_link_info;
  6762. if (!atomic_read(&nr_namespaces_events))
  6763. return;
  6764. namespaces_event = (struct perf_namespaces_event){
  6765. .task = task,
  6766. .event_id = {
  6767. .header = {
  6768. .type = PERF_RECORD_NAMESPACES,
  6769. .misc = 0,
  6770. .size = sizeof(namespaces_event.event_id),
  6771. },
  6772. /* .pid */
  6773. /* .tid */
  6774. .nr_namespaces = NR_NAMESPACES,
  6775. /* .link_info[NR_NAMESPACES] */
  6776. },
  6777. };
  6778. ns_link_info = namespaces_event.event_id.link_info;
  6779. perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX],
  6780. task, &mntns_operations);
  6781. #ifdef CONFIG_USER_NS
  6782. perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX],
  6783. task, &userns_operations);
  6784. #endif
  6785. #ifdef CONFIG_NET_NS
  6786. perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX],
  6787. task, &netns_operations);
  6788. #endif
  6789. #ifdef CONFIG_UTS_NS
  6790. perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX],
  6791. task, &utsns_operations);
  6792. #endif
  6793. #ifdef CONFIG_IPC_NS
  6794. perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX],
  6795. task, &ipcns_operations);
  6796. #endif
  6797. #ifdef CONFIG_PID_NS
  6798. perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX],
  6799. task, &pidns_operations);
  6800. #endif
  6801. #ifdef CONFIG_CGROUPS
  6802. perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX],
  6803. task, &cgroupns_operations);
  6804. #endif
  6805. perf_iterate_sb(perf_event_namespaces_output,
  6806. &namespaces_event,
  6807. NULL);
  6808. }
  6809. /*
  6810. * cgroup tracking
  6811. */
  6812. #ifdef CONFIG_CGROUP_PERF
  6813. struct perf_cgroup_event {
  6814. char *path;
  6815. int path_size;
  6816. struct {
  6817. struct perf_event_header header;
  6818. u64 id;
  6819. char path[];
  6820. } event_id;
  6821. };
  6822. static int perf_event_cgroup_match(struct perf_event *event)
  6823. {
  6824. return event->attr.cgroup;
  6825. }
  6826. static void perf_event_cgroup_output(struct perf_event *event, void *data)
  6827. {
  6828. struct perf_cgroup_event *cgroup_event = data;
  6829. struct perf_output_handle handle;
  6830. struct perf_sample_data sample;
  6831. u16 header_size = cgroup_event->event_id.header.size;
  6832. int ret;
  6833. if (!perf_event_cgroup_match(event))
  6834. return;
  6835. perf_event_header__init_id(&cgroup_event->event_id.header,
  6836. &sample, event);
  6837. ret = perf_output_begin(&handle, &sample, event,
  6838. cgroup_event->event_id.header.size);
  6839. if (ret)
  6840. goto out;
  6841. perf_output_put(&handle, cgroup_event->event_id);
  6842. __output_copy(&handle, cgroup_event->path, cgroup_event->path_size);
  6843. perf_event__output_id_sample(event, &handle, &sample);
  6844. perf_output_end(&handle);
  6845. out:
  6846. cgroup_event->event_id.header.size = header_size;
  6847. }
  6848. static void perf_event_cgroup(struct cgroup *cgrp)
  6849. {
  6850. struct perf_cgroup_event cgroup_event;
  6851. char path_enomem[16] = "//enomem";
  6852. char *pathname;
  6853. size_t size;
  6854. if (!atomic_read(&nr_cgroup_events))
  6855. return;
  6856. cgroup_event = (struct perf_cgroup_event){
  6857. .event_id = {
  6858. .header = {
  6859. .type = PERF_RECORD_CGROUP,
  6860. .misc = 0,
  6861. .size = sizeof(cgroup_event.event_id),
  6862. },
  6863. .id = cgroup_id(cgrp),
  6864. },
  6865. };
  6866. pathname = kmalloc(PATH_MAX, GFP_KERNEL);
  6867. if (pathname == NULL) {
  6868. cgroup_event.path = path_enomem;
  6869. } else {
  6870. /* just to be sure to have enough space for alignment */
  6871. cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64));
  6872. cgroup_event.path = pathname;
  6873. }
  6874. /*
  6875. * Since our buffer works in 8 byte units we need to align our string
  6876. * size to a multiple of 8. However, we must guarantee the tail end is
  6877. * zero'd out to avoid leaking random bits to userspace.
  6878. */
  6879. size = strlen(cgroup_event.path) + 1;
  6880. while (!IS_ALIGNED(size, sizeof(u64)))
  6881. cgroup_event.path[size++] = '\0';
  6882. cgroup_event.event_id.header.size += size;
  6883. cgroup_event.path_size = size;
  6884. perf_iterate_sb(perf_event_cgroup_output,
  6885. &cgroup_event,
  6886. NULL);
  6887. kfree(pathname);
  6888. }
  6889. #endif
  6890. /*
  6891. * mmap tracking
  6892. */
  6893. struct perf_mmap_event {
  6894. struct vm_area_struct *vma;
  6895. const char *file_name;
  6896. int file_size;
  6897. int maj, min;
  6898. u64 ino;
  6899. u64 ino_generation;
  6900. u32 prot, flags;
  6901. u8 build_id[BUILD_ID_SIZE_MAX];
  6902. u32 build_id_size;
  6903. struct {
  6904. struct perf_event_header header;
  6905. u32 pid;
  6906. u32 tid;
  6907. u64 start;
  6908. u64 len;
  6909. u64 pgoff;
  6910. } event_id;
  6911. };
  6912. static int perf_event_mmap_match(struct perf_event *event,
  6913. void *data)
  6914. {
  6915. struct perf_mmap_event *mmap_event = data;
  6916. struct vm_area_struct *vma = mmap_event->vma;
  6917. int executable = vma->vm_flags & VM_EXEC;
  6918. return (!executable && event->attr.mmap_data) ||
  6919. (executable && (event->attr.mmap || event->attr.mmap2));
  6920. }
  6921. static void perf_event_mmap_output(struct perf_event *event,
  6922. void *data)
  6923. {
  6924. struct perf_mmap_event *mmap_event = data;
  6925. struct perf_output_handle handle;
  6926. struct perf_sample_data sample;
  6927. int size = mmap_event->event_id.header.size;
  6928. u32 type = mmap_event->event_id.header.type;
  6929. bool use_build_id;
  6930. int ret;
  6931. if (!perf_event_mmap_match(event, data))
  6932. return;
  6933. if (event->attr.mmap2) {
  6934. mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
  6935. mmap_event->event_id.header.size += sizeof(mmap_event->maj);
  6936. mmap_event->event_id.header.size += sizeof(mmap_event->min);
  6937. mmap_event->event_id.header.size += sizeof(mmap_event->ino);
  6938. mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
  6939. mmap_event->event_id.header.size += sizeof(mmap_event->prot);
  6940. mmap_event->event_id.header.size += sizeof(mmap_event->flags);
  6941. }
  6942. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  6943. ret = perf_output_begin(&handle, &sample, event,
  6944. mmap_event->event_id.header.size);
  6945. if (ret)
  6946. goto out;
  6947. mmap_event->event_id.pid = perf_event_pid(event, current);
  6948. mmap_event->event_id.tid = perf_event_tid(event, current);
  6949. use_build_id = event->attr.build_id && mmap_event->build_id_size;
  6950. if (event->attr.mmap2 && use_build_id)
  6951. mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID;
  6952. perf_output_put(&handle, mmap_event->event_id);
  6953. if (event->attr.mmap2) {
  6954. if (use_build_id) {
  6955. u8 size[4] = { (u8) mmap_event->build_id_size, 0, 0, 0 };
  6956. __output_copy(&handle, size, 4);
  6957. __output_copy(&handle, mmap_event->build_id, BUILD_ID_SIZE_MAX);
  6958. } else {
  6959. perf_output_put(&handle, mmap_event->maj);
  6960. perf_output_put(&handle, mmap_event->min);
  6961. perf_output_put(&handle, mmap_event->ino);
  6962. perf_output_put(&handle, mmap_event->ino_generation);
  6963. }
  6964. perf_output_put(&handle, mmap_event->prot);
  6965. perf_output_put(&handle, mmap_event->flags);
  6966. }
  6967. __output_copy(&handle, mmap_event->file_name,
  6968. mmap_event->file_size);
  6969. perf_event__output_id_sample(event, &handle, &sample);
  6970. perf_output_end(&handle);
  6971. out:
  6972. mmap_event->event_id.header.size = size;
  6973. mmap_event->event_id.header.type = type;
  6974. }
  6975. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  6976. {
  6977. struct vm_area_struct *vma = mmap_event->vma;
  6978. struct file *file = vma->vm_file;
  6979. int maj = 0, min = 0;
  6980. u64 ino = 0, gen = 0;
  6981. u32 prot = 0, flags = 0;
  6982. unsigned int size;
  6983. char tmp[16];
  6984. char *buf = NULL;
  6985. char *name;
  6986. if (vma->vm_flags & VM_READ)
  6987. prot |= PROT_READ;
  6988. if (vma->vm_flags & VM_WRITE)
  6989. prot |= PROT_WRITE;
  6990. if (vma->vm_flags & VM_EXEC)
  6991. prot |= PROT_EXEC;
  6992. if (vma->vm_flags & VM_MAYSHARE)
  6993. flags = MAP_SHARED;
  6994. else
  6995. flags = MAP_PRIVATE;
  6996. if (vma->vm_flags & VM_LOCKED)
  6997. flags |= MAP_LOCKED;
  6998. if (is_vm_hugetlb_page(vma))
  6999. flags |= MAP_HUGETLB;
  7000. if (file) {
  7001. struct inode *inode;
  7002. dev_t dev;
  7003. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  7004. if (!buf) {
  7005. name = "//enomem";
  7006. goto cpy_name;
  7007. }
  7008. /*
  7009. * d_path() works from the end of the rb backwards, so we
  7010. * need to add enough zero bytes after the string to handle
  7011. * the 64bit alignment we do later.
  7012. */
  7013. name = file_path(file, buf, PATH_MAX - sizeof(u64));
  7014. if (IS_ERR(name)) {
  7015. name = "//toolong";
  7016. goto cpy_name;
  7017. }
  7018. inode = file_inode(vma->vm_file);
  7019. dev = inode->i_sb->s_dev;
  7020. ino = inode->i_ino;
  7021. gen = inode->i_generation;
  7022. maj = MAJOR(dev);
  7023. min = MINOR(dev);
  7024. goto got_name;
  7025. } else {
  7026. if (vma->vm_ops && vma->vm_ops->name) {
  7027. name = (char *) vma->vm_ops->name(vma);
  7028. if (name)
  7029. goto cpy_name;
  7030. }
  7031. name = (char *)arch_vma_name(vma);
  7032. if (name)
  7033. goto cpy_name;
  7034. if (vma->vm_start <= vma->vm_mm->start_brk &&
  7035. vma->vm_end >= vma->vm_mm->brk) {
  7036. name = "[heap]";
  7037. goto cpy_name;
  7038. }
  7039. if (vma->vm_start <= vma->vm_mm->start_stack &&
  7040. vma->vm_end >= vma->vm_mm->start_stack) {
  7041. name = "[stack]";
  7042. goto cpy_name;
  7043. }
  7044. name = "//anon";
  7045. goto cpy_name;
  7046. }
  7047. cpy_name:
  7048. strlcpy(tmp, name, sizeof(tmp));
  7049. name = tmp;
  7050. got_name:
  7051. /*
  7052. * Since our buffer works in 8 byte units we need to align our string
  7053. * size to a multiple of 8. However, we must guarantee the tail end is
  7054. * zero'd out to avoid leaking random bits to userspace.
  7055. */
  7056. size = strlen(name)+1;
  7057. while (!IS_ALIGNED(size, sizeof(u64)))
  7058. name[size++] = '\0';
  7059. mmap_event->file_name = name;
  7060. mmap_event->file_size = size;
  7061. mmap_event->maj = maj;
  7062. mmap_event->min = min;
  7063. mmap_event->ino = ino;
  7064. mmap_event->ino_generation = gen;
  7065. mmap_event->prot = prot;
  7066. mmap_event->flags = flags;
  7067. if (!(vma->vm_flags & VM_EXEC))
  7068. mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  7069. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  7070. if (atomic_read(&nr_build_id_events))
  7071. build_id_parse(vma, mmap_event->build_id, &mmap_event->build_id_size);
  7072. perf_iterate_sb(perf_event_mmap_output,
  7073. mmap_event,
  7074. NULL);
  7075. kfree(buf);
  7076. }
  7077. /*
  7078. * Check whether inode and address range match filter criteria.
  7079. */
  7080. static bool perf_addr_filter_match(struct perf_addr_filter *filter,
  7081. struct file *file, unsigned long offset,
  7082. unsigned long size)
  7083. {
  7084. /* d_inode(NULL) won't be equal to any mapped user-space file */
  7085. if (!filter->path.dentry)
  7086. return false;
  7087. if (d_inode(filter->path.dentry) != file_inode(file))
  7088. return false;
  7089. if (filter->offset > offset + size)
  7090. return false;
  7091. if (filter->offset + filter->size < offset)
  7092. return false;
  7093. return true;
  7094. }
  7095. static bool perf_addr_filter_vma_adjust(struct perf_addr_filter *filter,
  7096. struct vm_area_struct *vma,
  7097. struct perf_addr_filter_range *fr)
  7098. {
  7099. unsigned long vma_size = vma->vm_end - vma->vm_start;
  7100. unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
  7101. struct file *file = vma->vm_file;
  7102. if (!perf_addr_filter_match(filter, file, off, vma_size))
  7103. return false;
  7104. if (filter->offset < off) {
  7105. fr->start = vma->vm_start;
  7106. fr->size = min(vma_size, filter->size - (off - filter->offset));
  7107. } else {
  7108. fr->start = vma->vm_start + filter->offset - off;
  7109. fr->size = min(vma->vm_end - fr->start, filter->size);
  7110. }
  7111. return true;
  7112. }
  7113. static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
  7114. {
  7115. struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
  7116. struct vm_area_struct *vma = data;
  7117. struct perf_addr_filter *filter;
  7118. unsigned int restart = 0, count = 0;
  7119. unsigned long flags;
  7120. if (!has_addr_filter(event))
  7121. return;
  7122. if (!vma->vm_file)
  7123. return;
  7124. raw_spin_lock_irqsave(&ifh->lock, flags);
  7125. list_for_each_entry(filter, &ifh->list, entry) {
  7126. if (perf_addr_filter_vma_adjust(filter, vma,
  7127. &event->addr_filter_ranges[count]))
  7128. restart++;
  7129. count++;
  7130. }
  7131. if (restart)
  7132. event->addr_filters_gen++;
  7133. raw_spin_unlock_irqrestore(&ifh->lock, flags);
  7134. if (restart)
  7135. perf_event_stop(event, 1);
  7136. }
  7137. /*
  7138. * Adjust all task's events' filters to the new vma
  7139. */
  7140. static void perf_addr_filters_adjust(struct vm_area_struct *vma)
  7141. {
  7142. struct perf_event_context *ctx;
  7143. int ctxn;
  7144. /*
  7145. * Data tracing isn't supported yet and as such there is no need
  7146. * to keep track of anything that isn't related to executable code:
  7147. */
  7148. if (!(vma->vm_flags & VM_EXEC))
  7149. return;
  7150. rcu_read_lock();
  7151. for_each_task_context_nr(ctxn) {
  7152. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  7153. if (!ctx)
  7154. continue;
  7155. perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
  7156. }
  7157. rcu_read_unlock();
  7158. }
  7159. void perf_event_mmap(struct vm_area_struct *vma)
  7160. {
  7161. struct perf_mmap_event mmap_event;
  7162. if (!atomic_read(&nr_mmap_events))
  7163. return;
  7164. mmap_event = (struct perf_mmap_event){
  7165. .vma = vma,
  7166. /* .file_name */
  7167. /* .file_size */
  7168. .event_id = {
  7169. .header = {
  7170. .type = PERF_RECORD_MMAP,
  7171. .misc = PERF_RECORD_MISC_USER,
  7172. /* .size */
  7173. },
  7174. /* .pid */
  7175. /* .tid */
  7176. .start = vma->vm_start,
  7177. .len = vma->vm_end - vma->vm_start,
  7178. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  7179. },
  7180. /* .maj (attr_mmap2 only) */
  7181. /* .min (attr_mmap2 only) */
  7182. /* .ino (attr_mmap2 only) */
  7183. /* .ino_generation (attr_mmap2 only) */
  7184. /* .prot (attr_mmap2 only) */
  7185. /* .flags (attr_mmap2 only) */
  7186. };
  7187. perf_addr_filters_adjust(vma);
  7188. perf_event_mmap_event(&mmap_event);
  7189. }
  7190. void perf_event_aux_event(struct perf_event *event, unsigned long head,
  7191. unsigned long size, u64 flags)
  7192. {
  7193. struct perf_output_handle handle;
  7194. struct perf_sample_data sample;
  7195. struct perf_aux_event {
  7196. struct perf_event_header header;
  7197. u64 offset;
  7198. u64 size;
  7199. u64 flags;
  7200. } rec = {
  7201. .header = {
  7202. .type = PERF_RECORD_AUX,
  7203. .misc = 0,
  7204. .size = sizeof(rec),
  7205. },
  7206. .offset = head,
  7207. .size = size,
  7208. .flags = flags,
  7209. };
  7210. int ret;
  7211. perf_event_header__init_id(&rec.header, &sample, event);
  7212. ret = perf_output_begin(&handle, &sample, event, rec.header.size);
  7213. if (ret)
  7214. return;
  7215. perf_output_put(&handle, rec);
  7216. perf_event__output_id_sample(event, &handle, &sample);
  7217. perf_output_end(&handle);
  7218. }
  7219. /*
  7220. * Lost/dropped samples logging
  7221. */
  7222. void perf_log_lost_samples(struct perf_event *event, u64 lost)
  7223. {
  7224. struct perf_output_handle handle;
  7225. struct perf_sample_data sample;
  7226. int ret;
  7227. struct {
  7228. struct perf_event_header header;
  7229. u64 lost;
  7230. } lost_samples_event = {
  7231. .header = {
  7232. .type = PERF_RECORD_LOST_SAMPLES,
  7233. .misc = 0,
  7234. .size = sizeof(lost_samples_event),
  7235. },
  7236. .lost = lost,
  7237. };
  7238. perf_event_header__init_id(&lost_samples_event.header, &sample, event);
  7239. ret = perf_output_begin(&handle, &sample, event,
  7240. lost_samples_event.header.size);
  7241. if (ret)
  7242. return;
  7243. perf_output_put(&handle, lost_samples_event);
  7244. perf_event__output_id_sample(event, &handle, &sample);
  7245. perf_output_end(&handle);
  7246. }
  7247. /*
  7248. * context_switch tracking
  7249. */
  7250. struct perf_switch_event {
  7251. struct task_struct *task;
  7252. struct task_struct *next_prev;
  7253. struct {
  7254. struct perf_event_header header;
  7255. u32 next_prev_pid;
  7256. u32 next_prev_tid;
  7257. } event_id;
  7258. };
  7259. static int perf_event_switch_match(struct perf_event *event)
  7260. {
  7261. return event->attr.context_switch;
  7262. }
  7263. static void perf_event_switch_output(struct perf_event *event, void *data)
  7264. {
  7265. struct perf_switch_event *se = data;
  7266. struct perf_output_handle handle;
  7267. struct perf_sample_data sample;
  7268. int ret;
  7269. if (!perf_event_switch_match(event))
  7270. return;
  7271. /* Only CPU-wide events are allowed to see next/prev pid/tid */
  7272. if (event->ctx->task) {
  7273. se->event_id.header.type = PERF_RECORD_SWITCH;
  7274. se->event_id.header.size = sizeof(se->event_id.header);
  7275. } else {
  7276. se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
  7277. se->event_id.header.size = sizeof(se->event_id);
  7278. se->event_id.next_prev_pid =
  7279. perf_event_pid(event, se->next_prev);
  7280. se->event_id.next_prev_tid =
  7281. perf_event_tid(event, se->next_prev);
  7282. }
  7283. perf_event_header__init_id(&se->event_id.header, &sample, event);
  7284. ret = perf_output_begin(&handle, &sample, event, se->event_id.header.size);
  7285. if (ret)
  7286. return;
  7287. if (event->ctx->task)
  7288. perf_output_put(&handle, se->event_id.header);
  7289. else
  7290. perf_output_put(&handle, se->event_id);
  7291. perf_event__output_id_sample(event, &handle, &sample);
  7292. perf_output_end(&handle);
  7293. }
  7294. static void perf_event_switch(struct task_struct *task,
  7295. struct task_struct *next_prev, bool sched_in)
  7296. {
  7297. struct perf_switch_event switch_event;
  7298. /* N.B. caller checks nr_switch_events != 0 */
  7299. switch_event = (struct perf_switch_event){
  7300. .task = task,
  7301. .next_prev = next_prev,
  7302. .event_id = {
  7303. .header = {
  7304. /* .type */
  7305. .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
  7306. /* .size */
  7307. },
  7308. /* .next_prev_pid */
  7309. /* .next_prev_tid */
  7310. },
  7311. };
  7312. if (!sched_in && task->on_rq) {
  7313. switch_event.event_id.header.misc |=
  7314. PERF_RECORD_MISC_SWITCH_OUT_PREEMPT;
  7315. }
  7316. perf_iterate_sb(perf_event_switch_output, &switch_event, NULL);
  7317. }
  7318. /*
  7319. * IRQ throttle logging
  7320. */
  7321. static void perf_log_throttle(struct perf_event *event, int enable)
  7322. {
  7323. struct perf_output_handle handle;
  7324. struct perf_sample_data sample;
  7325. int ret;
  7326. struct {
  7327. struct perf_event_header header;
  7328. u64 time;
  7329. u64 id;
  7330. u64 stream_id;
  7331. } throttle_event = {
  7332. .header = {
  7333. .type = PERF_RECORD_THROTTLE,
  7334. .misc = 0,
  7335. .size = sizeof(throttle_event),
  7336. },
  7337. .time = perf_event_clock(event),
  7338. .id = primary_event_id(event),
  7339. .stream_id = event->id,
  7340. };
  7341. if (enable)
  7342. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  7343. perf_event_header__init_id(&throttle_event.header, &sample, event);
  7344. ret = perf_output_begin(&handle, &sample, event,
  7345. throttle_event.header.size);
  7346. if (ret)
  7347. return;
  7348. perf_output_put(&handle, throttle_event);
  7349. perf_event__output_id_sample(event, &handle, &sample);
  7350. perf_output_end(&handle);
  7351. }
  7352. /*
  7353. * ksymbol register/unregister tracking
  7354. */
  7355. struct perf_ksymbol_event {
  7356. const char *name;
  7357. int name_len;
  7358. struct {
  7359. struct perf_event_header header;
  7360. u64 addr;
  7361. u32 len;
  7362. u16 ksym_type;
  7363. u16 flags;
  7364. } event_id;
  7365. };
  7366. static int perf_event_ksymbol_match(struct perf_event *event)
  7367. {
  7368. return event->attr.ksymbol;
  7369. }
  7370. static void perf_event_ksymbol_output(struct perf_event *event, void *data)
  7371. {
  7372. struct perf_ksymbol_event *ksymbol_event = data;
  7373. struct perf_output_handle handle;
  7374. struct perf_sample_data sample;
  7375. int ret;
  7376. if (!perf_event_ksymbol_match(event))
  7377. return;
  7378. perf_event_header__init_id(&ksymbol_event->event_id.header,
  7379. &sample, event);
  7380. ret = perf_output_begin(&handle, &sample, event,
  7381. ksymbol_event->event_id.header.size);
  7382. if (ret)
  7383. return;
  7384. perf_output_put(&handle, ksymbol_event->event_id);
  7385. __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len);
  7386. perf_event__output_id_sample(event, &handle, &sample);
  7387. perf_output_end(&handle);
  7388. }
  7389. void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister,
  7390. const char *sym)
  7391. {
  7392. struct perf_ksymbol_event ksymbol_event;
  7393. char name[KSYM_NAME_LEN];
  7394. u16 flags = 0;
  7395. int name_len;
  7396. if (!atomic_read(&nr_ksymbol_events))
  7397. return;
  7398. if (ksym_type >= PERF_RECORD_KSYMBOL_TYPE_MAX ||
  7399. ksym_type == PERF_RECORD_KSYMBOL_TYPE_UNKNOWN)
  7400. goto err;
  7401. strlcpy(name, sym, KSYM_NAME_LEN);
  7402. name_len = strlen(name) + 1;
  7403. while (!IS_ALIGNED(name_len, sizeof(u64)))
  7404. name[name_len++] = '\0';
  7405. BUILD_BUG_ON(KSYM_NAME_LEN % sizeof(u64));
  7406. if (unregister)
  7407. flags |= PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER;
  7408. ksymbol_event = (struct perf_ksymbol_event){
  7409. .name = name,
  7410. .name_len = name_len,
  7411. .event_id = {
  7412. .header = {
  7413. .type = PERF_RECORD_KSYMBOL,
  7414. .size = sizeof(ksymbol_event.event_id) +
  7415. name_len,
  7416. },
  7417. .addr = addr,
  7418. .len = len,
  7419. .ksym_type = ksym_type,
  7420. .flags = flags,
  7421. },
  7422. };
  7423. perf_iterate_sb(perf_event_ksymbol_output, &ksymbol_event, NULL);
  7424. return;
  7425. err:
  7426. WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type);
  7427. }
  7428. /*
  7429. * bpf program load/unload tracking
  7430. */
  7431. struct perf_bpf_event {
  7432. struct bpf_prog *prog;
  7433. struct {
  7434. struct perf_event_header header;
  7435. u16 type;
  7436. u16 flags;
  7437. u32 id;
  7438. u8 tag[BPF_TAG_SIZE];
  7439. } event_id;
  7440. };
  7441. static int perf_event_bpf_match(struct perf_event *event)
  7442. {
  7443. return event->attr.bpf_event;
  7444. }
  7445. static void perf_event_bpf_output(struct perf_event *event, void *data)
  7446. {
  7447. struct perf_bpf_event *bpf_event = data;
  7448. struct perf_output_handle handle;
  7449. struct perf_sample_data sample;
  7450. int ret;
  7451. if (!perf_event_bpf_match(event))
  7452. return;
  7453. perf_event_header__init_id(&bpf_event->event_id.header,
  7454. &sample, event);
  7455. ret = perf_output_begin(&handle, &sample, event,
  7456. bpf_event->event_id.header.size);
  7457. if (ret)
  7458. return;
  7459. perf_output_put(&handle, bpf_event->event_id);
  7460. perf_event__output_id_sample(event, &handle, &sample);
  7461. perf_output_end(&handle);
  7462. }
  7463. static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog,
  7464. enum perf_bpf_event_type type)
  7465. {
  7466. bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD;
  7467. int i;
  7468. if (prog->aux->func_cnt == 0) {
  7469. perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF,
  7470. (u64)(unsigned long)prog->bpf_func,
  7471. prog->jited_len, unregister,
  7472. prog->aux->ksym.name);
  7473. } else {
  7474. for (i = 0; i < prog->aux->func_cnt; i++) {
  7475. struct bpf_prog *subprog = prog->aux->func[i];
  7476. perf_event_ksymbol(
  7477. PERF_RECORD_KSYMBOL_TYPE_BPF,
  7478. (u64)(unsigned long)subprog->bpf_func,
  7479. subprog->jited_len, unregister,
  7480. subprog->aux->ksym.name);
  7481. }
  7482. }
  7483. }
  7484. void perf_event_bpf_event(struct bpf_prog *prog,
  7485. enum perf_bpf_event_type type,
  7486. u16 flags)
  7487. {
  7488. struct perf_bpf_event bpf_event;
  7489. if (type <= PERF_BPF_EVENT_UNKNOWN ||
  7490. type >= PERF_BPF_EVENT_MAX)
  7491. return;
  7492. switch (type) {
  7493. case PERF_BPF_EVENT_PROG_LOAD:
  7494. case PERF_BPF_EVENT_PROG_UNLOAD:
  7495. if (atomic_read(&nr_ksymbol_events))
  7496. perf_event_bpf_emit_ksymbols(prog, type);
  7497. break;
  7498. default:
  7499. break;
  7500. }
  7501. if (!atomic_read(&nr_bpf_events))
  7502. return;
  7503. bpf_event = (struct perf_bpf_event){
  7504. .prog = prog,
  7505. .event_id = {
  7506. .header = {
  7507. .type = PERF_RECORD_BPF_EVENT,
  7508. .size = sizeof(bpf_event.event_id),
  7509. },
  7510. .type = type,
  7511. .flags = flags,
  7512. .id = prog->aux->id,
  7513. },
  7514. };
  7515. BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64));
  7516. memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE);
  7517. perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL);
  7518. }
  7519. struct perf_text_poke_event {
  7520. const void *old_bytes;
  7521. const void *new_bytes;
  7522. size_t pad;
  7523. u16 old_len;
  7524. u16 new_len;
  7525. struct {
  7526. struct perf_event_header header;
  7527. u64 addr;
  7528. } event_id;
  7529. };
  7530. static int perf_event_text_poke_match(struct perf_event *event)
  7531. {
  7532. return event->attr.text_poke;
  7533. }
  7534. static void perf_event_text_poke_output(struct perf_event *event, void *data)
  7535. {
  7536. struct perf_text_poke_event *text_poke_event = data;
  7537. struct perf_output_handle handle;
  7538. struct perf_sample_data sample;
  7539. u64 padding = 0;
  7540. int ret;
  7541. if (!perf_event_text_poke_match(event))
  7542. return;
  7543. perf_event_header__init_id(&text_poke_event->event_id.header, &sample, event);
  7544. ret = perf_output_begin(&handle, &sample, event,
  7545. text_poke_event->event_id.header.size);
  7546. if (ret)
  7547. return;
  7548. perf_output_put(&handle, text_poke_event->event_id);
  7549. perf_output_put(&handle, text_poke_event->old_len);
  7550. perf_output_put(&handle, text_poke_event->new_len);
  7551. __output_copy(&handle, text_poke_event->old_bytes, text_poke_event->old_len);
  7552. __output_copy(&handle, text_poke_event->new_bytes, text_poke_event->new_len);
  7553. if (text_poke_event->pad)
  7554. __output_copy(&handle, &padding, text_poke_event->pad);
  7555. perf_event__output_id_sample(event, &handle, &sample);
  7556. perf_output_end(&handle);
  7557. }
  7558. void perf_event_text_poke(const void *addr, const void *old_bytes,
  7559. size_t old_len, const void *new_bytes, size_t new_len)
  7560. {
  7561. struct perf_text_poke_event text_poke_event;
  7562. size_t tot, pad;
  7563. if (!atomic_read(&nr_text_poke_events))
  7564. return;
  7565. tot = sizeof(text_poke_event.old_len) + old_len;
  7566. tot += sizeof(text_poke_event.new_len) + new_len;
  7567. pad = ALIGN(tot, sizeof(u64)) - tot;
  7568. text_poke_event = (struct perf_text_poke_event){
  7569. .old_bytes = old_bytes,
  7570. .new_bytes = new_bytes,
  7571. .pad = pad,
  7572. .old_len = old_len,
  7573. .new_len = new_len,
  7574. .event_id = {
  7575. .header = {
  7576. .type = PERF_RECORD_TEXT_POKE,
  7577. .misc = PERF_RECORD_MISC_KERNEL,
  7578. .size = sizeof(text_poke_event.event_id) + tot + pad,
  7579. },
  7580. .addr = (unsigned long)addr,
  7581. },
  7582. };
  7583. perf_iterate_sb(perf_event_text_poke_output, &text_poke_event, NULL);
  7584. }
  7585. void perf_event_itrace_started(struct perf_event *event)
  7586. {
  7587. event->attach_state |= PERF_ATTACH_ITRACE;
  7588. }
  7589. static void perf_log_itrace_start(struct perf_event *event)
  7590. {
  7591. struct perf_output_handle handle;
  7592. struct perf_sample_data sample;
  7593. struct perf_aux_event {
  7594. struct perf_event_header header;
  7595. u32 pid;
  7596. u32 tid;
  7597. } rec;
  7598. int ret;
  7599. if (event->parent)
  7600. event = event->parent;
  7601. if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
  7602. event->attach_state & PERF_ATTACH_ITRACE)
  7603. return;
  7604. rec.header.type = PERF_RECORD_ITRACE_START;
  7605. rec.header.misc = 0;
  7606. rec.header.size = sizeof(rec);
  7607. rec.pid = perf_event_pid(event, current);
  7608. rec.tid = perf_event_tid(event, current);
  7609. perf_event_header__init_id(&rec.header, &sample, event);
  7610. ret = perf_output_begin(&handle, &sample, event, rec.header.size);
  7611. if (ret)
  7612. return;
  7613. perf_output_put(&handle, rec);
  7614. perf_event__output_id_sample(event, &handle, &sample);
  7615. perf_output_end(&handle);
  7616. }
  7617. void perf_report_aux_output_id(struct perf_event *event, u64 hw_id)
  7618. {
  7619. struct perf_output_handle handle;
  7620. struct perf_sample_data sample;
  7621. struct perf_aux_event {
  7622. struct perf_event_header header;
  7623. u64 hw_id;
  7624. } rec;
  7625. int ret;
  7626. if (event->parent)
  7627. event = event->parent;
  7628. rec.header.type = PERF_RECORD_AUX_OUTPUT_HW_ID;
  7629. rec.header.misc = 0;
  7630. rec.header.size = sizeof(rec);
  7631. rec.hw_id = hw_id;
  7632. perf_event_header__init_id(&rec.header, &sample, event);
  7633. ret = perf_output_begin(&handle, &sample, event, rec.header.size);
  7634. if (ret)
  7635. return;
  7636. perf_output_put(&handle, rec);
  7637. perf_event__output_id_sample(event, &handle, &sample);
  7638. perf_output_end(&handle);
  7639. }
  7640. static int
  7641. __perf_event_account_interrupt(struct perf_event *event, int throttle)
  7642. {
  7643. struct hw_perf_event *hwc = &event->hw;
  7644. int ret = 0;
  7645. u64 seq;
  7646. seq = __this_cpu_read(perf_throttled_seq);
  7647. if (seq != hwc->interrupts_seq) {
  7648. hwc->interrupts_seq = seq;
  7649. hwc->interrupts = 1;
  7650. } else {
  7651. hwc->interrupts++;
  7652. if (unlikely(throttle &&
  7653. hwc->interrupts > max_samples_per_tick)) {
  7654. __this_cpu_inc(perf_throttled_count);
  7655. tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
  7656. hwc->interrupts = MAX_INTERRUPTS;
  7657. perf_log_throttle(event, 0);
  7658. ret = 1;
  7659. }
  7660. }
  7661. if (event->attr.freq) {
  7662. u64 now = perf_clock();
  7663. s64 delta = now - hwc->freq_time_stamp;
  7664. hwc->freq_time_stamp = now;
  7665. if (delta > 0 && delta < 2*TICK_NSEC)
  7666. perf_adjust_period(event, delta, hwc->last_period, true);
  7667. }
  7668. return ret;
  7669. }
  7670. int perf_event_account_interrupt(struct perf_event *event)
  7671. {
  7672. return __perf_event_account_interrupt(event, 1);
  7673. }
  7674. static inline bool sample_is_allowed(struct perf_event *event, struct pt_regs *regs)
  7675. {
  7676. /*
  7677. * Due to interrupt latency (AKA "skid"), we may enter the
  7678. * kernel before taking an overflow, even if the PMU is only
  7679. * counting user events.
  7680. */
  7681. if (event->attr.exclude_kernel && !user_mode(regs))
  7682. return false;
  7683. return true;
  7684. }
  7685. /*
  7686. * Generic event overflow handling, sampling.
  7687. */
  7688. static int __perf_event_overflow(struct perf_event *event,
  7689. int throttle, struct perf_sample_data *data,
  7690. struct pt_regs *regs)
  7691. {
  7692. int events = atomic_read(&event->event_limit);
  7693. int ret = 0;
  7694. /*
  7695. * Non-sampling counters might still use the PMI to fold short
  7696. * hardware counters, ignore those.
  7697. */
  7698. if (unlikely(!is_sampling_event(event)))
  7699. return 0;
  7700. ret = __perf_event_account_interrupt(event, throttle);
  7701. /*
  7702. * XXX event_limit might not quite work as expected on inherited
  7703. * events
  7704. */
  7705. event->pending_kill = POLL_IN;
  7706. if (events && atomic_dec_and_test(&event->event_limit)) {
  7707. ret = 1;
  7708. event->pending_kill = POLL_HUP;
  7709. perf_event_disable_inatomic(event);
  7710. }
  7711. if (event->attr.sigtrap) {
  7712. /*
  7713. * The desired behaviour of sigtrap vs invalid samples is a bit
  7714. * tricky; on the one hand, one should not loose the SIGTRAP if
  7715. * it is the first event, on the other hand, we should also not
  7716. * trigger the WARN or override the data address.
  7717. */
  7718. bool valid_sample = sample_is_allowed(event, regs);
  7719. unsigned int pending_id = 1;
  7720. if (regs)
  7721. pending_id = hash32_ptr((void *)instruction_pointer(regs)) ?: 1;
  7722. if (!event->pending_sigtrap) {
  7723. event->pending_sigtrap = pending_id;
  7724. local_inc(&event->ctx->nr_pending);
  7725. } else if (event->attr.exclude_kernel && valid_sample) {
  7726. /*
  7727. * Should not be able to return to user space without
  7728. * consuming pending_sigtrap; with exceptions:
  7729. *
  7730. * 1. Where !exclude_kernel, events can overflow again
  7731. * in the kernel without returning to user space.
  7732. *
  7733. * 2. Events that can overflow again before the IRQ-
  7734. * work without user space progress (e.g. hrtimer).
  7735. * To approximate progress (with false negatives),
  7736. * check 32-bit hash of the current IP.
  7737. */
  7738. WARN_ON_ONCE(event->pending_sigtrap != pending_id);
  7739. }
  7740. event->pending_addr = 0;
  7741. if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR))
  7742. event->pending_addr = data->addr;
  7743. irq_work_queue(&event->pending_irq);
  7744. }
  7745. READ_ONCE(event->overflow_handler)(event, data, regs);
  7746. if (*perf_event_fasync(event) && event->pending_kill) {
  7747. event->pending_wakeup = 1;
  7748. irq_work_queue(&event->pending_irq);
  7749. }
  7750. return ret;
  7751. }
  7752. int perf_event_overflow(struct perf_event *event,
  7753. struct perf_sample_data *data,
  7754. struct pt_regs *regs)
  7755. {
  7756. return __perf_event_overflow(event, 1, data, regs);
  7757. }
  7758. /*
  7759. * Generic software event infrastructure
  7760. */
  7761. struct swevent_htable {
  7762. struct swevent_hlist *swevent_hlist;
  7763. struct mutex hlist_mutex;
  7764. int hlist_refcount;
  7765. /* Recursion avoidance in each contexts */
  7766. int recursion[PERF_NR_CONTEXTS];
  7767. };
  7768. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  7769. /*
  7770. * We directly increment event->count and keep a second value in
  7771. * event->hw.period_left to count intervals. This period event
  7772. * is kept in the range [-sample_period, 0] so that we can use the
  7773. * sign as trigger.
  7774. */
  7775. u64 perf_swevent_set_period(struct perf_event *event)
  7776. {
  7777. struct hw_perf_event *hwc = &event->hw;
  7778. u64 period = hwc->last_period;
  7779. u64 nr, offset;
  7780. s64 old, val;
  7781. hwc->last_period = hwc->sample_period;
  7782. again:
  7783. old = val = local64_read(&hwc->period_left);
  7784. if (val < 0)
  7785. return 0;
  7786. nr = div64_u64(period + val, period);
  7787. offset = nr * period;
  7788. val -= offset;
  7789. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  7790. goto again;
  7791. return nr;
  7792. }
  7793. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  7794. struct perf_sample_data *data,
  7795. struct pt_regs *regs)
  7796. {
  7797. struct hw_perf_event *hwc = &event->hw;
  7798. int throttle = 0;
  7799. if (!overflow)
  7800. overflow = perf_swevent_set_period(event);
  7801. if (hwc->interrupts == MAX_INTERRUPTS)
  7802. return;
  7803. for (; overflow; overflow--) {
  7804. if (__perf_event_overflow(event, throttle,
  7805. data, regs)) {
  7806. /*
  7807. * We inhibit the overflow from happening when
  7808. * hwc->interrupts == MAX_INTERRUPTS.
  7809. */
  7810. break;
  7811. }
  7812. throttle = 1;
  7813. }
  7814. }
  7815. static void perf_swevent_event(struct perf_event *event, u64 nr,
  7816. struct perf_sample_data *data,
  7817. struct pt_regs *regs)
  7818. {
  7819. struct hw_perf_event *hwc = &event->hw;
  7820. local64_add(nr, &event->count);
  7821. if (!regs)
  7822. return;
  7823. if (!is_sampling_event(event))
  7824. return;
  7825. if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
  7826. data->period = nr;
  7827. return perf_swevent_overflow(event, 1, data, regs);
  7828. } else
  7829. data->period = event->hw.last_period;
  7830. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  7831. return perf_swevent_overflow(event, 1, data, regs);
  7832. if (local64_add_negative(nr, &hwc->period_left))
  7833. return;
  7834. perf_swevent_overflow(event, 0, data, regs);
  7835. }
  7836. static int perf_exclude_event(struct perf_event *event,
  7837. struct pt_regs *regs)
  7838. {
  7839. if (event->hw.state & PERF_HES_STOPPED)
  7840. return 1;
  7841. if (regs) {
  7842. if (event->attr.exclude_user && user_mode(regs))
  7843. return 1;
  7844. if (event->attr.exclude_kernel && !user_mode(regs))
  7845. return 1;
  7846. }
  7847. return 0;
  7848. }
  7849. static int perf_swevent_match(struct perf_event *event,
  7850. enum perf_type_id type,
  7851. u32 event_id,
  7852. struct perf_sample_data *data,
  7853. struct pt_regs *regs)
  7854. {
  7855. if (event->attr.type != type)
  7856. return 0;
  7857. if (event->attr.config != event_id)
  7858. return 0;
  7859. if (perf_exclude_event(event, regs))
  7860. return 0;
  7861. return 1;
  7862. }
  7863. static inline u64 swevent_hash(u64 type, u32 event_id)
  7864. {
  7865. u64 val = event_id | (type << 32);
  7866. return hash_64(val, SWEVENT_HLIST_BITS);
  7867. }
  7868. static inline struct hlist_head *
  7869. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  7870. {
  7871. u64 hash = swevent_hash(type, event_id);
  7872. return &hlist->heads[hash];
  7873. }
  7874. /* For the read side: events when they trigger */
  7875. static inline struct hlist_head *
  7876. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  7877. {
  7878. struct swevent_hlist *hlist;
  7879. hlist = rcu_dereference(swhash->swevent_hlist);
  7880. if (!hlist)
  7881. return NULL;
  7882. return __find_swevent_head(hlist, type, event_id);
  7883. }
  7884. /* For the event head insertion and removal in the hlist */
  7885. static inline struct hlist_head *
  7886. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  7887. {
  7888. struct swevent_hlist *hlist;
  7889. u32 event_id = event->attr.config;
  7890. u64 type = event->attr.type;
  7891. /*
  7892. * Event scheduling is always serialized against hlist allocation
  7893. * and release. Which makes the protected version suitable here.
  7894. * The context lock guarantees that.
  7895. */
  7896. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  7897. lockdep_is_held(&event->ctx->lock));
  7898. if (!hlist)
  7899. return NULL;
  7900. return __find_swevent_head(hlist, type, event_id);
  7901. }
  7902. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  7903. u64 nr,
  7904. struct perf_sample_data *data,
  7905. struct pt_regs *regs)
  7906. {
  7907. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  7908. struct perf_event *event;
  7909. struct hlist_head *head;
  7910. rcu_read_lock();
  7911. head = find_swevent_head_rcu(swhash, type, event_id);
  7912. if (!head)
  7913. goto end;
  7914. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  7915. if (perf_swevent_match(event, type, event_id, data, regs))
  7916. perf_swevent_event(event, nr, data, regs);
  7917. }
  7918. end:
  7919. rcu_read_unlock();
  7920. }
  7921. DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
  7922. int perf_swevent_get_recursion_context(void)
  7923. {
  7924. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  7925. return get_recursion_context(swhash->recursion);
  7926. }
  7927. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  7928. void perf_swevent_put_recursion_context(int rctx)
  7929. {
  7930. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  7931. put_recursion_context(swhash->recursion, rctx);
  7932. }
  7933. void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  7934. {
  7935. struct perf_sample_data data;
  7936. if (WARN_ON_ONCE(!regs))
  7937. return;
  7938. perf_sample_data_init(&data, addr, 0);
  7939. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
  7940. }
  7941. void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  7942. {
  7943. int rctx;
  7944. preempt_disable_notrace();
  7945. rctx = perf_swevent_get_recursion_context();
  7946. if (unlikely(rctx < 0))
  7947. goto fail;
  7948. ___perf_sw_event(event_id, nr, regs, addr);
  7949. perf_swevent_put_recursion_context(rctx);
  7950. fail:
  7951. preempt_enable_notrace();
  7952. }
  7953. static void perf_swevent_read(struct perf_event *event)
  7954. {
  7955. }
  7956. static int perf_swevent_add(struct perf_event *event, int flags)
  7957. {
  7958. struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
  7959. struct hw_perf_event *hwc = &event->hw;
  7960. struct hlist_head *head;
  7961. if (is_sampling_event(event)) {
  7962. hwc->last_period = hwc->sample_period;
  7963. perf_swevent_set_period(event);
  7964. }
  7965. hwc->state = !(flags & PERF_EF_START);
  7966. head = find_swevent_head(swhash, event);
  7967. if (WARN_ON_ONCE(!head))
  7968. return -EINVAL;
  7969. hlist_add_head_rcu(&event->hlist_entry, head);
  7970. perf_event_update_userpage(event);
  7971. return 0;
  7972. }
  7973. static void perf_swevent_del(struct perf_event *event, int flags)
  7974. {
  7975. hlist_del_rcu(&event->hlist_entry);
  7976. }
  7977. static void perf_swevent_start(struct perf_event *event, int flags)
  7978. {
  7979. event->hw.state = 0;
  7980. }
  7981. static void perf_swevent_stop(struct perf_event *event, int flags)
  7982. {
  7983. event->hw.state = PERF_HES_STOPPED;
  7984. }
  7985. /* Deref the hlist from the update side */
  7986. static inline struct swevent_hlist *
  7987. swevent_hlist_deref(struct swevent_htable *swhash)
  7988. {
  7989. return rcu_dereference_protected(swhash->swevent_hlist,
  7990. lockdep_is_held(&swhash->hlist_mutex));
  7991. }
  7992. static void swevent_hlist_release(struct swevent_htable *swhash)
  7993. {
  7994. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  7995. if (!hlist)
  7996. return;
  7997. RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
  7998. kfree_rcu(hlist, rcu_head);
  7999. }
  8000. static void swevent_hlist_put_cpu(int cpu)
  8001. {
  8002. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  8003. mutex_lock(&swhash->hlist_mutex);
  8004. if (!--swhash->hlist_refcount)
  8005. swevent_hlist_release(swhash);
  8006. mutex_unlock(&swhash->hlist_mutex);
  8007. }
  8008. static void swevent_hlist_put(void)
  8009. {
  8010. int cpu;
  8011. for_each_possible_cpu(cpu)
  8012. swevent_hlist_put_cpu(cpu);
  8013. }
  8014. static int swevent_hlist_get_cpu(int cpu)
  8015. {
  8016. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  8017. int err = 0;
  8018. mutex_lock(&swhash->hlist_mutex);
  8019. if (!swevent_hlist_deref(swhash) &&
  8020. cpumask_test_cpu(cpu, perf_online_mask)) {
  8021. struct swevent_hlist *hlist;
  8022. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  8023. if (!hlist) {
  8024. err = -ENOMEM;
  8025. goto exit;
  8026. }
  8027. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  8028. }
  8029. swhash->hlist_refcount++;
  8030. exit:
  8031. mutex_unlock(&swhash->hlist_mutex);
  8032. return err;
  8033. }
  8034. static int swevent_hlist_get(void)
  8035. {
  8036. int err, cpu, failed_cpu;
  8037. mutex_lock(&pmus_lock);
  8038. for_each_possible_cpu(cpu) {
  8039. err = swevent_hlist_get_cpu(cpu);
  8040. if (err) {
  8041. failed_cpu = cpu;
  8042. goto fail;
  8043. }
  8044. }
  8045. mutex_unlock(&pmus_lock);
  8046. return 0;
  8047. fail:
  8048. for_each_possible_cpu(cpu) {
  8049. if (cpu == failed_cpu)
  8050. break;
  8051. swevent_hlist_put_cpu(cpu);
  8052. }
  8053. mutex_unlock(&pmus_lock);
  8054. return err;
  8055. }
  8056. struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  8057. static void sw_perf_event_destroy(struct perf_event *event)
  8058. {
  8059. u64 event_id = event->attr.config;
  8060. WARN_ON(event->parent);
  8061. static_key_slow_dec(&perf_swevent_enabled[event_id]);
  8062. swevent_hlist_put();
  8063. }
  8064. static int perf_swevent_init(struct perf_event *event)
  8065. {
  8066. u64 event_id = event->attr.config;
  8067. if (event->attr.type != PERF_TYPE_SOFTWARE)
  8068. return -ENOENT;
  8069. /*
  8070. * no branch sampling for software events
  8071. */
  8072. if (has_branch_stack(event))
  8073. return -EOPNOTSUPP;
  8074. switch (event_id) {
  8075. case PERF_COUNT_SW_CPU_CLOCK:
  8076. case PERF_COUNT_SW_TASK_CLOCK:
  8077. return -ENOENT;
  8078. default:
  8079. break;
  8080. }
  8081. if (event_id >= PERF_COUNT_SW_MAX)
  8082. return -ENOENT;
  8083. if (!event->parent) {
  8084. int err;
  8085. err = swevent_hlist_get();
  8086. if (err)
  8087. return err;
  8088. static_key_slow_inc(&perf_swevent_enabled[event_id]);
  8089. event->destroy = sw_perf_event_destroy;
  8090. }
  8091. return 0;
  8092. }
  8093. static struct pmu perf_swevent = {
  8094. .task_ctx_nr = perf_sw_context,
  8095. .capabilities = PERF_PMU_CAP_NO_NMI,
  8096. .event_init = perf_swevent_init,
  8097. .add = perf_swevent_add,
  8098. .del = perf_swevent_del,
  8099. .start = perf_swevent_start,
  8100. .stop = perf_swevent_stop,
  8101. .read = perf_swevent_read,
  8102. };
  8103. #ifdef CONFIG_EVENT_TRACING
  8104. static int perf_tp_filter_match(struct perf_event *event,
  8105. struct perf_sample_data *data)
  8106. {
  8107. void *record = data->raw->frag.data;
  8108. /* only top level events have filters set */
  8109. if (event->parent)
  8110. event = event->parent;
  8111. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  8112. return 1;
  8113. return 0;
  8114. }
  8115. static int perf_tp_event_match(struct perf_event *event,
  8116. struct perf_sample_data *data,
  8117. struct pt_regs *regs)
  8118. {
  8119. if (event->hw.state & PERF_HES_STOPPED)
  8120. return 0;
  8121. /*
  8122. * If exclude_kernel, only trace user-space tracepoints (uprobes)
  8123. */
  8124. if (event->attr.exclude_kernel && !user_mode(regs))
  8125. return 0;
  8126. if (!perf_tp_filter_match(event, data))
  8127. return 0;
  8128. return 1;
  8129. }
  8130. void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
  8131. struct trace_event_call *call, u64 count,
  8132. struct pt_regs *regs, struct hlist_head *head,
  8133. struct task_struct *task)
  8134. {
  8135. if (bpf_prog_array_valid(call)) {
  8136. *(struct pt_regs **)raw_data = regs;
  8137. if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) {
  8138. perf_swevent_put_recursion_context(rctx);
  8139. return;
  8140. }
  8141. }
  8142. perf_tp_event(call->event.type, count, raw_data, size, regs, head,
  8143. rctx, task);
  8144. }
  8145. EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
  8146. void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
  8147. struct pt_regs *regs, struct hlist_head *head, int rctx,
  8148. struct task_struct *task)
  8149. {
  8150. struct perf_sample_data data;
  8151. struct perf_event *event;
  8152. struct perf_raw_record raw = {
  8153. .frag = {
  8154. .size = entry_size,
  8155. .data = record,
  8156. },
  8157. };
  8158. perf_sample_data_init(&data, 0, 0);
  8159. data.raw = &raw;
  8160. data.sample_flags |= PERF_SAMPLE_RAW;
  8161. perf_trace_buf_update(record, event_type);
  8162. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  8163. if (perf_tp_event_match(event, &data, regs))
  8164. perf_swevent_event(event, count, &data, regs);
  8165. }
  8166. /*
  8167. * If we got specified a target task, also iterate its context and
  8168. * deliver this event there too.
  8169. */
  8170. if (task && task != current) {
  8171. struct perf_event_context *ctx;
  8172. struct trace_entry *entry = record;
  8173. rcu_read_lock();
  8174. ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
  8175. if (!ctx)
  8176. goto unlock;
  8177. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  8178. if (event->cpu != smp_processor_id())
  8179. continue;
  8180. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  8181. continue;
  8182. if (event->attr.config != entry->type)
  8183. continue;
  8184. /* Cannot deliver synchronous signal to other task. */
  8185. if (event->attr.sigtrap)
  8186. continue;
  8187. if (perf_tp_event_match(event, &data, regs))
  8188. perf_swevent_event(event, count, &data, regs);
  8189. }
  8190. unlock:
  8191. rcu_read_unlock();
  8192. }
  8193. perf_swevent_put_recursion_context(rctx);
  8194. }
  8195. EXPORT_SYMBOL_GPL(perf_tp_event);
  8196. static void tp_perf_event_destroy(struct perf_event *event)
  8197. {
  8198. perf_trace_destroy(event);
  8199. }
  8200. static int perf_tp_event_init(struct perf_event *event)
  8201. {
  8202. int err;
  8203. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  8204. return -ENOENT;
  8205. /*
  8206. * no branch sampling for tracepoint events
  8207. */
  8208. if (has_branch_stack(event))
  8209. return -EOPNOTSUPP;
  8210. err = perf_trace_init(event);
  8211. if (err)
  8212. return err;
  8213. event->destroy = tp_perf_event_destroy;
  8214. return 0;
  8215. }
  8216. static struct pmu perf_tracepoint = {
  8217. .task_ctx_nr = perf_sw_context,
  8218. .event_init = perf_tp_event_init,
  8219. .add = perf_trace_add,
  8220. .del = perf_trace_del,
  8221. .start = perf_swevent_start,
  8222. .stop = perf_swevent_stop,
  8223. .read = perf_swevent_read,
  8224. };
  8225. #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
  8226. /*
  8227. * Flags in config, used by dynamic PMU kprobe and uprobe
  8228. * The flags should match following PMU_FORMAT_ATTR().
  8229. *
  8230. * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe
  8231. * if not set, create kprobe/uprobe
  8232. *
  8233. * The following values specify a reference counter (or semaphore in the
  8234. * terminology of tools like dtrace, systemtap, etc.) Userspace Statically
  8235. * Defined Tracepoints (USDT). Currently, we use 40 bit for the offset.
  8236. *
  8237. * PERF_UPROBE_REF_CTR_OFFSET_BITS # of bits in config as th offset
  8238. * PERF_UPROBE_REF_CTR_OFFSET_SHIFT # of bits to shift left
  8239. */
  8240. enum perf_probe_config {
  8241. PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */
  8242. PERF_UPROBE_REF_CTR_OFFSET_BITS = 32,
  8243. PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS,
  8244. };
  8245. PMU_FORMAT_ATTR(retprobe, "config:0");
  8246. #endif
  8247. #ifdef CONFIG_KPROBE_EVENTS
  8248. static struct attribute *kprobe_attrs[] = {
  8249. &format_attr_retprobe.attr,
  8250. NULL,
  8251. };
  8252. static struct attribute_group kprobe_format_group = {
  8253. .name = "format",
  8254. .attrs = kprobe_attrs,
  8255. };
  8256. static const struct attribute_group *kprobe_attr_groups[] = {
  8257. &kprobe_format_group,
  8258. NULL,
  8259. };
  8260. static int perf_kprobe_event_init(struct perf_event *event);
  8261. static struct pmu perf_kprobe = {
  8262. .task_ctx_nr = perf_sw_context,
  8263. .event_init = perf_kprobe_event_init,
  8264. .add = perf_trace_add,
  8265. .del = perf_trace_del,
  8266. .start = perf_swevent_start,
  8267. .stop = perf_swevent_stop,
  8268. .read = perf_swevent_read,
  8269. .attr_groups = kprobe_attr_groups,
  8270. };
  8271. static int perf_kprobe_event_init(struct perf_event *event)
  8272. {
  8273. int err;
  8274. bool is_retprobe;
  8275. if (event->attr.type != perf_kprobe.type)
  8276. return -ENOENT;
  8277. if (!perfmon_capable())
  8278. return -EACCES;
  8279. /*
  8280. * no branch sampling for probe events
  8281. */
  8282. if (has_branch_stack(event))
  8283. return -EOPNOTSUPP;
  8284. is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
  8285. err = perf_kprobe_init(event, is_retprobe);
  8286. if (err)
  8287. return err;
  8288. event->destroy = perf_kprobe_destroy;
  8289. return 0;
  8290. }
  8291. #endif /* CONFIG_KPROBE_EVENTS */
  8292. #ifdef CONFIG_UPROBE_EVENTS
  8293. PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63");
  8294. static struct attribute *uprobe_attrs[] = {
  8295. &format_attr_retprobe.attr,
  8296. &format_attr_ref_ctr_offset.attr,
  8297. NULL,
  8298. };
  8299. static struct attribute_group uprobe_format_group = {
  8300. .name = "format",
  8301. .attrs = uprobe_attrs,
  8302. };
  8303. static const struct attribute_group *uprobe_attr_groups[] = {
  8304. &uprobe_format_group,
  8305. NULL,
  8306. };
  8307. static int perf_uprobe_event_init(struct perf_event *event);
  8308. static struct pmu perf_uprobe = {
  8309. .task_ctx_nr = perf_sw_context,
  8310. .event_init = perf_uprobe_event_init,
  8311. .add = perf_trace_add,
  8312. .del = perf_trace_del,
  8313. .start = perf_swevent_start,
  8314. .stop = perf_swevent_stop,
  8315. .read = perf_swevent_read,
  8316. .attr_groups = uprobe_attr_groups,
  8317. };
  8318. static int perf_uprobe_event_init(struct perf_event *event)
  8319. {
  8320. int err;
  8321. unsigned long ref_ctr_offset;
  8322. bool is_retprobe;
  8323. if (event->attr.type != perf_uprobe.type)
  8324. return -ENOENT;
  8325. if (!perfmon_capable())
  8326. return -EACCES;
  8327. /*
  8328. * no branch sampling for probe events
  8329. */
  8330. if (has_branch_stack(event))
  8331. return -EOPNOTSUPP;
  8332. is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
  8333. ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
  8334. err = perf_uprobe_init(event, ref_ctr_offset, is_retprobe);
  8335. if (err)
  8336. return err;
  8337. event->destroy = perf_uprobe_destroy;
  8338. return 0;
  8339. }
  8340. #endif /* CONFIG_UPROBE_EVENTS */
  8341. static inline void perf_tp_register(void)
  8342. {
  8343. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  8344. #ifdef CONFIG_KPROBE_EVENTS
  8345. perf_pmu_register(&perf_kprobe, "kprobe", -1);
  8346. #endif
  8347. #ifdef CONFIG_UPROBE_EVENTS
  8348. perf_pmu_register(&perf_uprobe, "uprobe", -1);
  8349. #endif
  8350. }
  8351. static void perf_event_free_filter(struct perf_event *event)
  8352. {
  8353. ftrace_profile_free_filter(event);
  8354. }
  8355. #ifdef CONFIG_BPF_SYSCALL
  8356. static void bpf_overflow_handler(struct perf_event *event,
  8357. struct perf_sample_data *data,
  8358. struct pt_regs *regs)
  8359. {
  8360. struct bpf_perf_event_data_kern ctx = {
  8361. .data = data,
  8362. .event = event,
  8363. };
  8364. struct bpf_prog *prog;
  8365. int ret = 0;
  8366. ctx.regs = perf_arch_bpf_user_pt_regs(regs);
  8367. if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
  8368. goto out;
  8369. rcu_read_lock();
  8370. prog = READ_ONCE(event->prog);
  8371. if (prog) {
  8372. if (prog->call_get_stack &&
  8373. (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) &&
  8374. !(data->sample_flags & PERF_SAMPLE_CALLCHAIN)) {
  8375. data->callchain = perf_callchain(event, regs);
  8376. data->sample_flags |= PERF_SAMPLE_CALLCHAIN;
  8377. }
  8378. ret = bpf_prog_run(prog, &ctx);
  8379. }
  8380. rcu_read_unlock();
  8381. out:
  8382. __this_cpu_dec(bpf_prog_active);
  8383. if (!ret)
  8384. return;
  8385. event->orig_overflow_handler(event, data, regs);
  8386. }
  8387. static int perf_event_set_bpf_handler(struct perf_event *event,
  8388. struct bpf_prog *prog,
  8389. u64 bpf_cookie)
  8390. {
  8391. if (event->overflow_handler_context)
  8392. /* hw breakpoint or kernel counter */
  8393. return -EINVAL;
  8394. if (event->prog)
  8395. return -EEXIST;
  8396. if (prog->type != BPF_PROG_TYPE_PERF_EVENT)
  8397. return -EINVAL;
  8398. if (event->attr.precise_ip &&
  8399. prog->call_get_stack &&
  8400. (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) ||
  8401. event->attr.exclude_callchain_kernel ||
  8402. event->attr.exclude_callchain_user)) {
  8403. /*
  8404. * On perf_event with precise_ip, calling bpf_get_stack()
  8405. * may trigger unwinder warnings and occasional crashes.
  8406. * bpf_get_[stack|stackid] works around this issue by using
  8407. * callchain attached to perf_sample_data. If the
  8408. * perf_event does not full (kernel and user) callchain
  8409. * attached to perf_sample_data, do not allow attaching BPF
  8410. * program that calls bpf_get_[stack|stackid].
  8411. */
  8412. return -EPROTO;
  8413. }
  8414. event->prog = prog;
  8415. event->bpf_cookie = bpf_cookie;
  8416. event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
  8417. WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
  8418. return 0;
  8419. }
  8420. static void perf_event_free_bpf_handler(struct perf_event *event)
  8421. {
  8422. struct bpf_prog *prog = event->prog;
  8423. if (!prog)
  8424. return;
  8425. WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
  8426. event->prog = NULL;
  8427. bpf_prog_put(prog);
  8428. }
  8429. #else
  8430. static int perf_event_set_bpf_handler(struct perf_event *event,
  8431. struct bpf_prog *prog,
  8432. u64 bpf_cookie)
  8433. {
  8434. return -EOPNOTSUPP;
  8435. }
  8436. static void perf_event_free_bpf_handler(struct perf_event *event)
  8437. {
  8438. }
  8439. #endif
  8440. /*
  8441. * returns true if the event is a tracepoint, or a kprobe/upprobe created
  8442. * with perf_event_open()
  8443. */
  8444. static inline bool perf_event_is_tracing(struct perf_event *event)
  8445. {
  8446. if (event->pmu == &perf_tracepoint)
  8447. return true;
  8448. #ifdef CONFIG_KPROBE_EVENTS
  8449. if (event->pmu == &perf_kprobe)
  8450. return true;
  8451. #endif
  8452. #ifdef CONFIG_UPROBE_EVENTS
  8453. if (event->pmu == &perf_uprobe)
  8454. return true;
  8455. #endif
  8456. return false;
  8457. }
  8458. int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog,
  8459. u64 bpf_cookie)
  8460. {
  8461. bool is_kprobe, is_uprobe, is_tracepoint, is_syscall_tp;
  8462. if (!perf_event_is_tracing(event))
  8463. return perf_event_set_bpf_handler(event, prog, bpf_cookie);
  8464. is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE;
  8465. is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE;
  8466. is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
  8467. is_syscall_tp = is_syscall_trace_event(event->tp_event);
  8468. if (!is_kprobe && !is_uprobe && !is_tracepoint && !is_syscall_tp)
  8469. /* bpf programs can only be attached to u/kprobe or tracepoint */
  8470. return -EINVAL;
  8471. if (((is_kprobe || is_uprobe) && prog->type != BPF_PROG_TYPE_KPROBE) ||
  8472. (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
  8473. (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT))
  8474. return -EINVAL;
  8475. if (prog->type == BPF_PROG_TYPE_KPROBE && prog->aux->sleepable && !is_uprobe)
  8476. /* only uprobe programs are allowed to be sleepable */
  8477. return -EINVAL;
  8478. /* Kprobe override only works for kprobes, not uprobes. */
  8479. if (prog->kprobe_override && !is_kprobe)
  8480. return -EINVAL;
  8481. if (is_tracepoint || is_syscall_tp) {
  8482. int off = trace_event_get_offsets(event->tp_event);
  8483. if (prog->aux->max_ctx_offset > off)
  8484. return -EACCES;
  8485. }
  8486. return perf_event_attach_bpf_prog(event, prog, bpf_cookie);
  8487. }
  8488. void perf_event_free_bpf_prog(struct perf_event *event)
  8489. {
  8490. if (!perf_event_is_tracing(event)) {
  8491. perf_event_free_bpf_handler(event);
  8492. return;
  8493. }
  8494. perf_event_detach_bpf_prog(event);
  8495. }
  8496. #else
  8497. static inline void perf_tp_register(void)
  8498. {
  8499. }
  8500. static void perf_event_free_filter(struct perf_event *event)
  8501. {
  8502. }
  8503. int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog,
  8504. u64 bpf_cookie)
  8505. {
  8506. return -ENOENT;
  8507. }
  8508. void perf_event_free_bpf_prog(struct perf_event *event)
  8509. {
  8510. }
  8511. #endif /* CONFIG_EVENT_TRACING */
  8512. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  8513. void perf_bp_event(struct perf_event *bp, void *data)
  8514. {
  8515. struct perf_sample_data sample;
  8516. struct pt_regs *regs = data;
  8517. perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
  8518. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  8519. perf_swevent_event(bp, 1, &sample, regs);
  8520. }
  8521. #endif
  8522. /*
  8523. * Allocate a new address filter
  8524. */
  8525. static struct perf_addr_filter *
  8526. perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
  8527. {
  8528. int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
  8529. struct perf_addr_filter *filter;
  8530. filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
  8531. if (!filter)
  8532. return NULL;
  8533. INIT_LIST_HEAD(&filter->entry);
  8534. list_add_tail(&filter->entry, filters);
  8535. return filter;
  8536. }
  8537. static void free_filters_list(struct list_head *filters)
  8538. {
  8539. struct perf_addr_filter *filter, *iter;
  8540. list_for_each_entry_safe(filter, iter, filters, entry) {
  8541. path_put(&filter->path);
  8542. list_del(&filter->entry);
  8543. kfree(filter);
  8544. }
  8545. }
  8546. /*
  8547. * Free existing address filters and optionally install new ones
  8548. */
  8549. static void perf_addr_filters_splice(struct perf_event *event,
  8550. struct list_head *head)
  8551. {
  8552. unsigned long flags;
  8553. LIST_HEAD(list);
  8554. if (!has_addr_filter(event))
  8555. return;
  8556. /* don't bother with children, they don't have their own filters */
  8557. if (event->parent)
  8558. return;
  8559. raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
  8560. list_splice_init(&event->addr_filters.list, &list);
  8561. if (head)
  8562. list_splice(head, &event->addr_filters.list);
  8563. raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
  8564. free_filters_list(&list);
  8565. }
  8566. /*
  8567. * Scan through mm's vmas and see if one of them matches the
  8568. * @filter; if so, adjust filter's address range.
  8569. * Called with mm::mmap_lock down for reading.
  8570. */
  8571. static void perf_addr_filter_apply(struct perf_addr_filter *filter,
  8572. struct mm_struct *mm,
  8573. struct perf_addr_filter_range *fr)
  8574. {
  8575. struct vm_area_struct *vma;
  8576. VMA_ITERATOR(vmi, mm, 0);
  8577. for_each_vma(vmi, vma) {
  8578. if (!vma->vm_file)
  8579. continue;
  8580. if (perf_addr_filter_vma_adjust(filter, vma, fr))
  8581. return;
  8582. }
  8583. }
  8584. /*
  8585. * Update event's address range filters based on the
  8586. * task's existing mappings, if any.
  8587. */
  8588. static void perf_event_addr_filters_apply(struct perf_event *event)
  8589. {
  8590. struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
  8591. struct task_struct *task = READ_ONCE(event->ctx->task);
  8592. struct perf_addr_filter *filter;
  8593. struct mm_struct *mm = NULL;
  8594. unsigned int count = 0;
  8595. unsigned long flags;
  8596. /*
  8597. * We may observe TASK_TOMBSTONE, which means that the event tear-down
  8598. * will stop on the parent's child_mutex that our caller is also holding
  8599. */
  8600. if (task == TASK_TOMBSTONE)
  8601. return;
  8602. if (ifh->nr_file_filters) {
  8603. mm = get_task_mm(task);
  8604. if (!mm)
  8605. goto restart;
  8606. mmap_read_lock(mm);
  8607. }
  8608. raw_spin_lock_irqsave(&ifh->lock, flags);
  8609. list_for_each_entry(filter, &ifh->list, entry) {
  8610. if (filter->path.dentry) {
  8611. /*
  8612. * Adjust base offset if the filter is associated to a
  8613. * binary that needs to be mapped:
  8614. */
  8615. event->addr_filter_ranges[count].start = 0;
  8616. event->addr_filter_ranges[count].size = 0;
  8617. perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]);
  8618. } else {
  8619. event->addr_filter_ranges[count].start = filter->offset;
  8620. event->addr_filter_ranges[count].size = filter->size;
  8621. }
  8622. count++;
  8623. }
  8624. event->addr_filters_gen++;
  8625. raw_spin_unlock_irqrestore(&ifh->lock, flags);
  8626. if (ifh->nr_file_filters) {
  8627. mmap_read_unlock(mm);
  8628. mmput(mm);
  8629. }
  8630. restart:
  8631. perf_event_stop(event, 1);
  8632. }
  8633. /*
  8634. * Address range filtering: limiting the data to certain
  8635. * instruction address ranges. Filters are ioctl()ed to us from
  8636. * userspace as ascii strings.
  8637. *
  8638. * Filter string format:
  8639. *
  8640. * ACTION RANGE_SPEC
  8641. * where ACTION is one of the
  8642. * * "filter": limit the trace to this region
  8643. * * "start": start tracing from this address
  8644. * * "stop": stop tracing at this address/region;
  8645. * RANGE_SPEC is
  8646. * * for kernel addresses: <start address>[/<size>]
  8647. * * for object files: <start address>[/<size>]@</path/to/object/file>
  8648. *
  8649. * if <size> is not specified or is zero, the range is treated as a single
  8650. * address; not valid for ACTION=="filter".
  8651. */
  8652. enum {
  8653. IF_ACT_NONE = -1,
  8654. IF_ACT_FILTER,
  8655. IF_ACT_START,
  8656. IF_ACT_STOP,
  8657. IF_SRC_FILE,
  8658. IF_SRC_KERNEL,
  8659. IF_SRC_FILEADDR,
  8660. IF_SRC_KERNELADDR,
  8661. };
  8662. enum {
  8663. IF_STATE_ACTION = 0,
  8664. IF_STATE_SOURCE,
  8665. IF_STATE_END,
  8666. };
  8667. static const match_table_t if_tokens = {
  8668. { IF_ACT_FILTER, "filter" },
  8669. { IF_ACT_START, "start" },
  8670. { IF_ACT_STOP, "stop" },
  8671. { IF_SRC_FILE, "%u/%u@%s" },
  8672. { IF_SRC_KERNEL, "%u/%u" },
  8673. { IF_SRC_FILEADDR, "%u@%s" },
  8674. { IF_SRC_KERNELADDR, "%u" },
  8675. { IF_ACT_NONE, NULL },
  8676. };
  8677. /*
  8678. * Address filter string parser
  8679. */
  8680. static int
  8681. perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
  8682. struct list_head *filters)
  8683. {
  8684. struct perf_addr_filter *filter = NULL;
  8685. char *start, *orig, *filename = NULL;
  8686. substring_t args[MAX_OPT_ARGS];
  8687. int state = IF_STATE_ACTION, token;
  8688. unsigned int kernel = 0;
  8689. int ret = -EINVAL;
  8690. orig = fstr = kstrdup(fstr, GFP_KERNEL);
  8691. if (!fstr)
  8692. return -ENOMEM;
  8693. while ((start = strsep(&fstr, " ,\n")) != NULL) {
  8694. static const enum perf_addr_filter_action_t actions[] = {
  8695. [IF_ACT_FILTER] = PERF_ADDR_FILTER_ACTION_FILTER,
  8696. [IF_ACT_START] = PERF_ADDR_FILTER_ACTION_START,
  8697. [IF_ACT_STOP] = PERF_ADDR_FILTER_ACTION_STOP,
  8698. };
  8699. ret = -EINVAL;
  8700. if (!*start)
  8701. continue;
  8702. /* filter definition begins */
  8703. if (state == IF_STATE_ACTION) {
  8704. filter = perf_addr_filter_new(event, filters);
  8705. if (!filter)
  8706. goto fail;
  8707. }
  8708. token = match_token(start, if_tokens, args);
  8709. switch (token) {
  8710. case IF_ACT_FILTER:
  8711. case IF_ACT_START:
  8712. case IF_ACT_STOP:
  8713. if (state != IF_STATE_ACTION)
  8714. goto fail;
  8715. filter->action = actions[token];
  8716. state = IF_STATE_SOURCE;
  8717. break;
  8718. case IF_SRC_KERNELADDR:
  8719. case IF_SRC_KERNEL:
  8720. kernel = 1;
  8721. fallthrough;
  8722. case IF_SRC_FILEADDR:
  8723. case IF_SRC_FILE:
  8724. if (state != IF_STATE_SOURCE)
  8725. goto fail;
  8726. *args[0].to = 0;
  8727. ret = kstrtoul(args[0].from, 0, &filter->offset);
  8728. if (ret)
  8729. goto fail;
  8730. if (token == IF_SRC_KERNEL || token == IF_SRC_FILE) {
  8731. *args[1].to = 0;
  8732. ret = kstrtoul(args[1].from, 0, &filter->size);
  8733. if (ret)
  8734. goto fail;
  8735. }
  8736. if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
  8737. int fpos = token == IF_SRC_FILE ? 2 : 1;
  8738. kfree(filename);
  8739. filename = match_strdup(&args[fpos]);
  8740. if (!filename) {
  8741. ret = -ENOMEM;
  8742. goto fail;
  8743. }
  8744. }
  8745. state = IF_STATE_END;
  8746. break;
  8747. default:
  8748. goto fail;
  8749. }
  8750. /*
  8751. * Filter definition is fully parsed, validate and install it.
  8752. * Make sure that it doesn't contradict itself or the event's
  8753. * attribute.
  8754. */
  8755. if (state == IF_STATE_END) {
  8756. ret = -EINVAL;
  8757. /*
  8758. * ACTION "filter" must have a non-zero length region
  8759. * specified.
  8760. */
  8761. if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER &&
  8762. !filter->size)
  8763. goto fail;
  8764. if (!kernel) {
  8765. if (!filename)
  8766. goto fail;
  8767. /*
  8768. * For now, we only support file-based filters
  8769. * in per-task events; doing so for CPU-wide
  8770. * events requires additional context switching
  8771. * trickery, since same object code will be
  8772. * mapped at different virtual addresses in
  8773. * different processes.
  8774. */
  8775. ret = -EOPNOTSUPP;
  8776. if (!event->ctx->task)
  8777. goto fail;
  8778. /* look up the path and grab its inode */
  8779. ret = kern_path(filename, LOOKUP_FOLLOW,
  8780. &filter->path);
  8781. if (ret)
  8782. goto fail;
  8783. ret = -EINVAL;
  8784. if (!filter->path.dentry ||
  8785. !S_ISREG(d_inode(filter->path.dentry)
  8786. ->i_mode))
  8787. goto fail;
  8788. event->addr_filters.nr_file_filters++;
  8789. }
  8790. /* ready to consume more filters */
  8791. kfree(filename);
  8792. filename = NULL;
  8793. state = IF_STATE_ACTION;
  8794. filter = NULL;
  8795. kernel = 0;
  8796. }
  8797. }
  8798. if (state != IF_STATE_ACTION)
  8799. goto fail;
  8800. kfree(filename);
  8801. kfree(orig);
  8802. return 0;
  8803. fail:
  8804. kfree(filename);
  8805. free_filters_list(filters);
  8806. kfree(orig);
  8807. return ret;
  8808. }
  8809. static int
  8810. perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
  8811. {
  8812. LIST_HEAD(filters);
  8813. int ret;
  8814. /*
  8815. * Since this is called in perf_ioctl() path, we're already holding
  8816. * ctx::mutex.
  8817. */
  8818. lockdep_assert_held(&event->ctx->mutex);
  8819. if (WARN_ON_ONCE(event->parent))
  8820. return -EINVAL;
  8821. ret = perf_event_parse_addr_filter(event, filter_str, &filters);
  8822. if (ret)
  8823. goto fail_clear_files;
  8824. ret = event->pmu->addr_filters_validate(&filters);
  8825. if (ret)
  8826. goto fail_free_filters;
  8827. /* remove existing filters, if any */
  8828. perf_addr_filters_splice(event, &filters);
  8829. /* install new filters */
  8830. perf_event_for_each_child(event, perf_event_addr_filters_apply);
  8831. return ret;
  8832. fail_free_filters:
  8833. free_filters_list(&filters);
  8834. fail_clear_files:
  8835. event->addr_filters.nr_file_filters = 0;
  8836. return ret;
  8837. }
  8838. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  8839. {
  8840. int ret = -EINVAL;
  8841. char *filter_str;
  8842. filter_str = strndup_user(arg, PAGE_SIZE);
  8843. if (IS_ERR(filter_str))
  8844. return PTR_ERR(filter_str);
  8845. #ifdef CONFIG_EVENT_TRACING
  8846. if (perf_event_is_tracing(event)) {
  8847. struct perf_event_context *ctx = event->ctx;
  8848. /*
  8849. * Beware, here be dragons!!
  8850. *
  8851. * the tracepoint muck will deadlock against ctx->mutex, but
  8852. * the tracepoint stuff does not actually need it. So
  8853. * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we
  8854. * already have a reference on ctx.
  8855. *
  8856. * This can result in event getting moved to a different ctx,
  8857. * but that does not affect the tracepoint state.
  8858. */
  8859. mutex_unlock(&ctx->mutex);
  8860. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  8861. mutex_lock(&ctx->mutex);
  8862. } else
  8863. #endif
  8864. if (has_addr_filter(event))
  8865. ret = perf_event_set_addr_filter(event, filter_str);
  8866. kfree(filter_str);
  8867. return ret;
  8868. }
  8869. /*
  8870. * hrtimer based swevent callback
  8871. */
  8872. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  8873. {
  8874. enum hrtimer_restart ret = HRTIMER_RESTART;
  8875. struct perf_sample_data data;
  8876. struct pt_regs *regs;
  8877. struct perf_event *event;
  8878. u64 period;
  8879. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  8880. if (event->state != PERF_EVENT_STATE_ACTIVE)
  8881. return HRTIMER_NORESTART;
  8882. event->pmu->read(event);
  8883. perf_sample_data_init(&data, 0, event->hw.last_period);
  8884. regs = get_irq_regs();
  8885. if (regs && !perf_exclude_event(event, regs)) {
  8886. if (!(event->attr.exclude_idle && is_idle_task(current)))
  8887. if (__perf_event_overflow(event, 1, &data, regs))
  8888. ret = HRTIMER_NORESTART;
  8889. }
  8890. period = max_t(u64, 10000, event->hw.sample_period);
  8891. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  8892. return ret;
  8893. }
  8894. static void perf_swevent_start_hrtimer(struct perf_event *event)
  8895. {
  8896. struct hw_perf_event *hwc = &event->hw;
  8897. s64 period;
  8898. if (!is_sampling_event(event))
  8899. return;
  8900. period = local64_read(&hwc->period_left);
  8901. if (period) {
  8902. if (period < 0)
  8903. period = 10000;
  8904. local64_set(&hwc->period_left, 0);
  8905. } else {
  8906. period = max_t(u64, 10000, hwc->sample_period);
  8907. }
  8908. hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
  8909. HRTIMER_MODE_REL_PINNED_HARD);
  8910. }
  8911. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  8912. {
  8913. struct hw_perf_event *hwc = &event->hw;
  8914. if (is_sampling_event(event)) {
  8915. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  8916. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  8917. hrtimer_cancel(&hwc->hrtimer);
  8918. }
  8919. }
  8920. static void perf_swevent_init_hrtimer(struct perf_event *event)
  8921. {
  8922. struct hw_perf_event *hwc = &event->hw;
  8923. if (!is_sampling_event(event))
  8924. return;
  8925. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  8926. hwc->hrtimer.function = perf_swevent_hrtimer;
  8927. /*
  8928. * Since hrtimers have a fixed rate, we can do a static freq->period
  8929. * mapping and avoid the whole period adjust feedback stuff.
  8930. */
  8931. if (event->attr.freq) {
  8932. long freq = event->attr.sample_freq;
  8933. event->attr.sample_period = NSEC_PER_SEC / freq;
  8934. hwc->sample_period = event->attr.sample_period;
  8935. local64_set(&hwc->period_left, hwc->sample_period);
  8936. hwc->last_period = hwc->sample_period;
  8937. event->attr.freq = 0;
  8938. }
  8939. }
  8940. /*
  8941. * Software event: cpu wall time clock
  8942. */
  8943. static void cpu_clock_event_update(struct perf_event *event)
  8944. {
  8945. s64 prev;
  8946. u64 now;
  8947. now = local_clock();
  8948. prev = local64_xchg(&event->hw.prev_count, now);
  8949. local64_add(now - prev, &event->count);
  8950. }
  8951. static void cpu_clock_event_start(struct perf_event *event, int flags)
  8952. {
  8953. local64_set(&event->hw.prev_count, local_clock());
  8954. perf_swevent_start_hrtimer(event);
  8955. }
  8956. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  8957. {
  8958. perf_swevent_cancel_hrtimer(event);
  8959. cpu_clock_event_update(event);
  8960. }
  8961. static int cpu_clock_event_add(struct perf_event *event, int flags)
  8962. {
  8963. if (flags & PERF_EF_START)
  8964. cpu_clock_event_start(event, flags);
  8965. perf_event_update_userpage(event);
  8966. return 0;
  8967. }
  8968. static void cpu_clock_event_del(struct perf_event *event, int flags)
  8969. {
  8970. cpu_clock_event_stop(event, flags);
  8971. }
  8972. static void cpu_clock_event_read(struct perf_event *event)
  8973. {
  8974. cpu_clock_event_update(event);
  8975. }
  8976. static int cpu_clock_event_init(struct perf_event *event)
  8977. {
  8978. if (event->attr.type != PERF_TYPE_SOFTWARE)
  8979. return -ENOENT;
  8980. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  8981. return -ENOENT;
  8982. /*
  8983. * no branch sampling for software events
  8984. */
  8985. if (has_branch_stack(event))
  8986. return -EOPNOTSUPP;
  8987. perf_swevent_init_hrtimer(event);
  8988. return 0;
  8989. }
  8990. static struct pmu perf_cpu_clock = {
  8991. .task_ctx_nr = perf_sw_context,
  8992. .capabilities = PERF_PMU_CAP_NO_NMI,
  8993. .event_init = cpu_clock_event_init,
  8994. .add = cpu_clock_event_add,
  8995. .del = cpu_clock_event_del,
  8996. .start = cpu_clock_event_start,
  8997. .stop = cpu_clock_event_stop,
  8998. .read = cpu_clock_event_read,
  8999. };
  9000. /*
  9001. * Software event: task time clock
  9002. */
  9003. static void task_clock_event_update(struct perf_event *event, u64 now)
  9004. {
  9005. u64 prev;
  9006. s64 delta;
  9007. prev = local64_xchg(&event->hw.prev_count, now);
  9008. delta = now - prev;
  9009. local64_add(delta, &event->count);
  9010. }
  9011. static void task_clock_event_start(struct perf_event *event, int flags)
  9012. {
  9013. local64_set(&event->hw.prev_count, event->ctx->time);
  9014. perf_swevent_start_hrtimer(event);
  9015. }
  9016. static void task_clock_event_stop(struct perf_event *event, int flags)
  9017. {
  9018. perf_swevent_cancel_hrtimer(event);
  9019. task_clock_event_update(event, event->ctx->time);
  9020. }
  9021. static int task_clock_event_add(struct perf_event *event, int flags)
  9022. {
  9023. if (flags & PERF_EF_START)
  9024. task_clock_event_start(event, flags);
  9025. perf_event_update_userpage(event);
  9026. return 0;
  9027. }
  9028. static void task_clock_event_del(struct perf_event *event, int flags)
  9029. {
  9030. task_clock_event_stop(event, PERF_EF_UPDATE);
  9031. }
  9032. static void task_clock_event_read(struct perf_event *event)
  9033. {
  9034. u64 now = perf_clock();
  9035. u64 delta = now - event->ctx->timestamp;
  9036. u64 time = event->ctx->time + delta;
  9037. task_clock_event_update(event, time);
  9038. }
  9039. static int task_clock_event_init(struct perf_event *event)
  9040. {
  9041. if (event->attr.type != PERF_TYPE_SOFTWARE)
  9042. return -ENOENT;
  9043. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  9044. return -ENOENT;
  9045. /*
  9046. * no branch sampling for software events
  9047. */
  9048. if (has_branch_stack(event))
  9049. return -EOPNOTSUPP;
  9050. perf_swevent_init_hrtimer(event);
  9051. return 0;
  9052. }
  9053. static struct pmu perf_task_clock = {
  9054. .task_ctx_nr = perf_sw_context,
  9055. .capabilities = PERF_PMU_CAP_NO_NMI,
  9056. .event_init = task_clock_event_init,
  9057. .add = task_clock_event_add,
  9058. .del = task_clock_event_del,
  9059. .start = task_clock_event_start,
  9060. .stop = task_clock_event_stop,
  9061. .read = task_clock_event_read,
  9062. };
  9063. static void perf_pmu_nop_void(struct pmu *pmu)
  9064. {
  9065. }
  9066. static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
  9067. {
  9068. }
  9069. static int perf_pmu_nop_int(struct pmu *pmu)
  9070. {
  9071. return 0;
  9072. }
  9073. static int perf_event_nop_int(struct perf_event *event, u64 value)
  9074. {
  9075. return 0;
  9076. }
  9077. static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
  9078. static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
  9079. {
  9080. __this_cpu_write(nop_txn_flags, flags);
  9081. if (flags & ~PERF_PMU_TXN_ADD)
  9082. return;
  9083. perf_pmu_disable(pmu);
  9084. }
  9085. static int perf_pmu_commit_txn(struct pmu *pmu)
  9086. {
  9087. unsigned int flags = __this_cpu_read(nop_txn_flags);
  9088. __this_cpu_write(nop_txn_flags, 0);
  9089. if (flags & ~PERF_PMU_TXN_ADD)
  9090. return 0;
  9091. perf_pmu_enable(pmu);
  9092. return 0;
  9093. }
  9094. static void perf_pmu_cancel_txn(struct pmu *pmu)
  9095. {
  9096. unsigned int flags = __this_cpu_read(nop_txn_flags);
  9097. __this_cpu_write(nop_txn_flags, 0);
  9098. if (flags & ~PERF_PMU_TXN_ADD)
  9099. return;
  9100. perf_pmu_enable(pmu);
  9101. }
  9102. static int perf_event_idx_default(struct perf_event *event)
  9103. {
  9104. return 0;
  9105. }
  9106. /*
  9107. * Ensures all contexts with the same task_ctx_nr have the same
  9108. * pmu_cpu_context too.
  9109. */
  9110. static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
  9111. {
  9112. struct pmu *pmu;
  9113. if (ctxn < 0)
  9114. return NULL;
  9115. list_for_each_entry(pmu, &pmus, entry) {
  9116. if (pmu->task_ctx_nr == ctxn)
  9117. return pmu->pmu_cpu_context;
  9118. }
  9119. return NULL;
  9120. }
  9121. static void free_pmu_context(struct pmu *pmu)
  9122. {
  9123. /*
  9124. * Static contexts such as perf_sw_context have a global lifetime
  9125. * and may be shared between different PMUs. Avoid freeing them
  9126. * when a single PMU is going away.
  9127. */
  9128. if (pmu->task_ctx_nr > perf_invalid_context)
  9129. return;
  9130. free_percpu(pmu->pmu_cpu_context);
  9131. }
  9132. /*
  9133. * Let userspace know that this PMU supports address range filtering:
  9134. */
  9135. static ssize_t nr_addr_filters_show(struct device *dev,
  9136. struct device_attribute *attr,
  9137. char *page)
  9138. {
  9139. struct pmu *pmu = dev_get_drvdata(dev);
  9140. return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
  9141. }
  9142. DEVICE_ATTR_RO(nr_addr_filters);
  9143. static struct idr pmu_idr;
  9144. static ssize_t
  9145. type_show(struct device *dev, struct device_attribute *attr, char *page)
  9146. {
  9147. struct pmu *pmu = dev_get_drvdata(dev);
  9148. return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->type);
  9149. }
  9150. static DEVICE_ATTR_RO(type);
  9151. static ssize_t
  9152. perf_event_mux_interval_ms_show(struct device *dev,
  9153. struct device_attribute *attr,
  9154. char *page)
  9155. {
  9156. struct pmu *pmu = dev_get_drvdata(dev);
  9157. return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->hrtimer_interval_ms);
  9158. }
  9159. static DEFINE_MUTEX(mux_interval_mutex);
  9160. static ssize_t
  9161. perf_event_mux_interval_ms_store(struct device *dev,
  9162. struct device_attribute *attr,
  9163. const char *buf, size_t count)
  9164. {
  9165. struct pmu *pmu = dev_get_drvdata(dev);
  9166. int timer, cpu, ret;
  9167. ret = kstrtoint(buf, 0, &timer);
  9168. if (ret)
  9169. return ret;
  9170. if (timer < 1)
  9171. return -EINVAL;
  9172. /* same value, noting to do */
  9173. if (timer == pmu->hrtimer_interval_ms)
  9174. return count;
  9175. mutex_lock(&mux_interval_mutex);
  9176. pmu->hrtimer_interval_ms = timer;
  9177. /* update all cpuctx for this PMU */
  9178. cpus_read_lock();
  9179. for_each_online_cpu(cpu) {
  9180. struct perf_cpu_context *cpuctx;
  9181. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  9182. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
  9183. cpu_function_call(cpu, perf_mux_hrtimer_restart_ipi, cpuctx);
  9184. }
  9185. cpus_read_unlock();
  9186. mutex_unlock(&mux_interval_mutex);
  9187. return count;
  9188. }
  9189. static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
  9190. static struct attribute *pmu_dev_attrs[] = {
  9191. &dev_attr_type.attr,
  9192. &dev_attr_perf_event_mux_interval_ms.attr,
  9193. NULL,
  9194. };
  9195. ATTRIBUTE_GROUPS(pmu_dev);
  9196. static int pmu_bus_running;
  9197. static struct bus_type pmu_bus = {
  9198. .name = "event_source",
  9199. .dev_groups = pmu_dev_groups,
  9200. };
  9201. static void pmu_dev_release(struct device *dev)
  9202. {
  9203. kfree(dev);
  9204. }
  9205. static int pmu_dev_alloc(struct pmu *pmu)
  9206. {
  9207. int ret = -ENOMEM;
  9208. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  9209. if (!pmu->dev)
  9210. goto out;
  9211. pmu->dev->groups = pmu->attr_groups;
  9212. device_initialize(pmu->dev);
  9213. dev_set_drvdata(pmu->dev, pmu);
  9214. pmu->dev->bus = &pmu_bus;
  9215. pmu->dev->release = pmu_dev_release;
  9216. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  9217. if (ret)
  9218. goto free_dev;
  9219. ret = device_add(pmu->dev);
  9220. if (ret)
  9221. goto free_dev;
  9222. /* For PMUs with address filters, throw in an extra attribute: */
  9223. if (pmu->nr_addr_filters)
  9224. ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
  9225. if (ret)
  9226. goto del_dev;
  9227. if (pmu->attr_update)
  9228. ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update);
  9229. if (ret)
  9230. goto del_dev;
  9231. out:
  9232. return ret;
  9233. del_dev:
  9234. device_del(pmu->dev);
  9235. free_dev:
  9236. put_device(pmu->dev);
  9237. goto out;
  9238. }
  9239. static struct lock_class_key cpuctx_mutex;
  9240. static struct lock_class_key cpuctx_lock;
  9241. int perf_pmu_register(struct pmu *pmu, const char *name, int type)
  9242. {
  9243. int cpu, ret, max = PERF_TYPE_MAX;
  9244. mutex_lock(&pmus_lock);
  9245. ret = -ENOMEM;
  9246. pmu->pmu_disable_count = alloc_percpu(int);
  9247. if (!pmu->pmu_disable_count)
  9248. goto unlock;
  9249. pmu->type = -1;
  9250. if (!name)
  9251. goto skip_type;
  9252. pmu->name = name;
  9253. if (type != PERF_TYPE_SOFTWARE) {
  9254. if (type >= 0)
  9255. max = type;
  9256. ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL);
  9257. if (ret < 0)
  9258. goto free_pdc;
  9259. WARN_ON(type >= 0 && ret != type);
  9260. type = ret;
  9261. }
  9262. pmu->type = type;
  9263. if (pmu_bus_running) {
  9264. ret = pmu_dev_alloc(pmu);
  9265. if (ret)
  9266. goto free_idr;
  9267. }
  9268. skip_type:
  9269. if (pmu->task_ctx_nr == perf_hw_context) {
  9270. static int hw_context_taken = 0;
  9271. /*
  9272. * Other than systems with heterogeneous CPUs, it never makes
  9273. * sense for two PMUs to share perf_hw_context. PMUs which are
  9274. * uncore must use perf_invalid_context.
  9275. */
  9276. if (WARN_ON_ONCE(hw_context_taken &&
  9277. !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
  9278. pmu->task_ctx_nr = perf_invalid_context;
  9279. hw_context_taken = 1;
  9280. }
  9281. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  9282. if (pmu->pmu_cpu_context)
  9283. goto got_cpu_context;
  9284. ret = -ENOMEM;
  9285. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  9286. if (!pmu->pmu_cpu_context)
  9287. goto free_dev;
  9288. for_each_possible_cpu(cpu) {
  9289. struct perf_cpu_context *cpuctx;
  9290. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  9291. __perf_event_init_context(&cpuctx->ctx);
  9292. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  9293. lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
  9294. cpuctx->ctx.pmu = pmu;
  9295. cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask);
  9296. __perf_mux_hrtimer_init(cpuctx, cpu);
  9297. cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default);
  9298. cpuctx->heap = cpuctx->heap_default;
  9299. }
  9300. got_cpu_context:
  9301. if (!pmu->start_txn) {
  9302. if (pmu->pmu_enable) {
  9303. /*
  9304. * If we have pmu_enable/pmu_disable calls, install
  9305. * transaction stubs that use that to try and batch
  9306. * hardware accesses.
  9307. */
  9308. pmu->start_txn = perf_pmu_start_txn;
  9309. pmu->commit_txn = perf_pmu_commit_txn;
  9310. pmu->cancel_txn = perf_pmu_cancel_txn;
  9311. } else {
  9312. pmu->start_txn = perf_pmu_nop_txn;
  9313. pmu->commit_txn = perf_pmu_nop_int;
  9314. pmu->cancel_txn = perf_pmu_nop_void;
  9315. }
  9316. }
  9317. if (!pmu->pmu_enable) {
  9318. pmu->pmu_enable = perf_pmu_nop_void;
  9319. pmu->pmu_disable = perf_pmu_nop_void;
  9320. }
  9321. if (!pmu->check_period)
  9322. pmu->check_period = perf_event_nop_int;
  9323. if (!pmu->event_idx)
  9324. pmu->event_idx = perf_event_idx_default;
  9325. /*
  9326. * Ensure the TYPE_SOFTWARE PMUs are at the head of the list,
  9327. * since these cannot be in the IDR. This way the linear search
  9328. * is fast, provided a valid software event is provided.
  9329. */
  9330. if (type == PERF_TYPE_SOFTWARE || !name)
  9331. list_add_rcu(&pmu->entry, &pmus);
  9332. else
  9333. list_add_tail_rcu(&pmu->entry, &pmus);
  9334. atomic_set(&pmu->exclusive_cnt, 0);
  9335. ret = 0;
  9336. unlock:
  9337. mutex_unlock(&pmus_lock);
  9338. return ret;
  9339. free_dev:
  9340. device_del(pmu->dev);
  9341. put_device(pmu->dev);
  9342. free_idr:
  9343. if (pmu->type != PERF_TYPE_SOFTWARE)
  9344. idr_remove(&pmu_idr, pmu->type);
  9345. free_pdc:
  9346. free_percpu(pmu->pmu_disable_count);
  9347. goto unlock;
  9348. }
  9349. EXPORT_SYMBOL_GPL(perf_pmu_register);
  9350. void perf_pmu_unregister(struct pmu *pmu)
  9351. {
  9352. mutex_lock(&pmus_lock);
  9353. list_del_rcu(&pmu->entry);
  9354. /*
  9355. * We dereference the pmu list under both SRCU and regular RCU, so
  9356. * synchronize against both of those.
  9357. */
  9358. synchronize_srcu(&pmus_srcu);
  9359. synchronize_rcu();
  9360. free_percpu(pmu->pmu_disable_count);
  9361. if (pmu->type != PERF_TYPE_SOFTWARE)
  9362. idr_remove(&pmu_idr, pmu->type);
  9363. if (pmu_bus_running) {
  9364. if (pmu->nr_addr_filters)
  9365. device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
  9366. device_del(pmu->dev);
  9367. put_device(pmu->dev);
  9368. }
  9369. free_pmu_context(pmu);
  9370. mutex_unlock(&pmus_lock);
  9371. }
  9372. EXPORT_SYMBOL_GPL(perf_pmu_unregister);
  9373. static inline bool has_extended_regs(struct perf_event *event)
  9374. {
  9375. return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) ||
  9376. (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK);
  9377. }
  9378. static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
  9379. {
  9380. struct perf_event_context *ctx = NULL;
  9381. int ret;
  9382. if (!try_module_get(pmu->module))
  9383. return -ENODEV;
  9384. /*
  9385. * A number of pmu->event_init() methods iterate the sibling_list to,
  9386. * for example, validate if the group fits on the PMU. Therefore,
  9387. * if this is a sibling event, acquire the ctx->mutex to protect
  9388. * the sibling_list.
  9389. */
  9390. if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) {
  9391. /*
  9392. * This ctx->mutex can nest when we're called through
  9393. * inheritance. See the perf_event_ctx_lock_nested() comment.
  9394. */
  9395. ctx = perf_event_ctx_lock_nested(event->group_leader,
  9396. SINGLE_DEPTH_NESTING);
  9397. BUG_ON(!ctx);
  9398. }
  9399. event->pmu = pmu;
  9400. ret = pmu->event_init(event);
  9401. if (ctx)
  9402. perf_event_ctx_unlock(event->group_leader, ctx);
  9403. if (!ret) {
  9404. if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) &&
  9405. has_extended_regs(event))
  9406. ret = -EOPNOTSUPP;
  9407. if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE &&
  9408. event_has_any_exclude_flag(event))
  9409. ret = -EINVAL;
  9410. if (ret && event->destroy)
  9411. event->destroy(event);
  9412. }
  9413. if (ret)
  9414. module_put(pmu->module);
  9415. return ret;
  9416. }
  9417. static struct pmu *perf_init_event(struct perf_event *event)
  9418. {
  9419. bool extended_type = false;
  9420. int idx, type, ret;
  9421. struct pmu *pmu;
  9422. idx = srcu_read_lock(&pmus_srcu);
  9423. /* Try parent's PMU first: */
  9424. if (event->parent && event->parent->pmu) {
  9425. pmu = event->parent->pmu;
  9426. ret = perf_try_init_event(pmu, event);
  9427. if (!ret)
  9428. goto unlock;
  9429. }
  9430. /*
  9431. * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE
  9432. * are often aliases for PERF_TYPE_RAW.
  9433. */
  9434. type = event->attr.type;
  9435. if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) {
  9436. type = event->attr.config >> PERF_PMU_TYPE_SHIFT;
  9437. if (!type) {
  9438. type = PERF_TYPE_RAW;
  9439. } else {
  9440. extended_type = true;
  9441. event->attr.config &= PERF_HW_EVENT_MASK;
  9442. }
  9443. }
  9444. again:
  9445. rcu_read_lock();
  9446. pmu = idr_find(&pmu_idr, type);
  9447. rcu_read_unlock();
  9448. if (pmu) {
  9449. if (event->attr.type != type && type != PERF_TYPE_RAW &&
  9450. !(pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE))
  9451. goto fail;
  9452. ret = perf_try_init_event(pmu, event);
  9453. if (ret == -ENOENT && event->attr.type != type && !extended_type) {
  9454. type = event->attr.type;
  9455. goto again;
  9456. }
  9457. if (ret)
  9458. pmu = ERR_PTR(ret);
  9459. goto unlock;
  9460. }
  9461. list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) {
  9462. ret = perf_try_init_event(pmu, event);
  9463. if (!ret)
  9464. goto unlock;
  9465. if (ret != -ENOENT) {
  9466. pmu = ERR_PTR(ret);
  9467. goto unlock;
  9468. }
  9469. }
  9470. fail:
  9471. pmu = ERR_PTR(-ENOENT);
  9472. unlock:
  9473. srcu_read_unlock(&pmus_srcu, idx);
  9474. return pmu;
  9475. }
  9476. static void attach_sb_event(struct perf_event *event)
  9477. {
  9478. struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
  9479. raw_spin_lock(&pel->lock);
  9480. list_add_rcu(&event->sb_list, &pel->list);
  9481. raw_spin_unlock(&pel->lock);
  9482. }
  9483. /*
  9484. * We keep a list of all !task (and therefore per-cpu) events
  9485. * that need to receive side-band records.
  9486. *
  9487. * This avoids having to scan all the various PMU per-cpu contexts
  9488. * looking for them.
  9489. */
  9490. static void account_pmu_sb_event(struct perf_event *event)
  9491. {
  9492. if (is_sb_event(event))
  9493. attach_sb_event(event);
  9494. }
  9495. static void account_event_cpu(struct perf_event *event, int cpu)
  9496. {
  9497. if (event->parent)
  9498. return;
  9499. if (is_cgroup_event(event))
  9500. atomic_inc(&per_cpu(perf_cgroup_events, cpu));
  9501. }
  9502. /* Freq events need the tick to stay alive (see perf_event_task_tick). */
  9503. static void account_freq_event_nohz(void)
  9504. {
  9505. #ifdef CONFIG_NO_HZ_FULL
  9506. /* Lock so we don't race with concurrent unaccount */
  9507. spin_lock(&nr_freq_lock);
  9508. if (atomic_inc_return(&nr_freq_events) == 1)
  9509. tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
  9510. spin_unlock(&nr_freq_lock);
  9511. #endif
  9512. }
  9513. static void account_freq_event(void)
  9514. {
  9515. if (tick_nohz_full_enabled())
  9516. account_freq_event_nohz();
  9517. else
  9518. atomic_inc(&nr_freq_events);
  9519. }
  9520. static void account_event(struct perf_event *event)
  9521. {
  9522. bool inc = false;
  9523. if (event->parent)
  9524. return;
  9525. if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB))
  9526. inc = true;
  9527. if (event->attr.mmap || event->attr.mmap_data)
  9528. atomic_inc(&nr_mmap_events);
  9529. if (event->attr.build_id)
  9530. atomic_inc(&nr_build_id_events);
  9531. if (event->attr.comm)
  9532. atomic_inc(&nr_comm_events);
  9533. if (event->attr.namespaces)
  9534. atomic_inc(&nr_namespaces_events);
  9535. if (event->attr.cgroup)
  9536. atomic_inc(&nr_cgroup_events);
  9537. if (event->attr.task)
  9538. atomic_inc(&nr_task_events);
  9539. if (event->attr.freq)
  9540. account_freq_event();
  9541. if (event->attr.context_switch) {
  9542. atomic_inc(&nr_switch_events);
  9543. inc = true;
  9544. }
  9545. if (has_branch_stack(event))
  9546. inc = true;
  9547. if (is_cgroup_event(event))
  9548. inc = true;
  9549. if (event->attr.ksymbol)
  9550. atomic_inc(&nr_ksymbol_events);
  9551. if (event->attr.bpf_event)
  9552. atomic_inc(&nr_bpf_events);
  9553. if (event->attr.text_poke)
  9554. atomic_inc(&nr_text_poke_events);
  9555. if (inc) {
  9556. /*
  9557. * We need the mutex here because static_branch_enable()
  9558. * must complete *before* the perf_sched_count increment
  9559. * becomes visible.
  9560. */
  9561. if (atomic_inc_not_zero(&perf_sched_count))
  9562. goto enabled;
  9563. mutex_lock(&perf_sched_mutex);
  9564. if (!atomic_read(&perf_sched_count)) {
  9565. static_branch_enable(&perf_sched_events);
  9566. /*
  9567. * Guarantee that all CPUs observe they key change and
  9568. * call the perf scheduling hooks before proceeding to
  9569. * install events that need them.
  9570. */
  9571. synchronize_rcu();
  9572. }
  9573. /*
  9574. * Now that we have waited for the sync_sched(), allow further
  9575. * increments to by-pass the mutex.
  9576. */
  9577. atomic_inc(&perf_sched_count);
  9578. mutex_unlock(&perf_sched_mutex);
  9579. }
  9580. enabled:
  9581. account_event_cpu(event, event->cpu);
  9582. account_pmu_sb_event(event);
  9583. }
  9584. /*
  9585. * Allocate and initialize an event structure
  9586. */
  9587. static struct perf_event *
  9588. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  9589. struct task_struct *task,
  9590. struct perf_event *group_leader,
  9591. struct perf_event *parent_event,
  9592. perf_overflow_handler_t overflow_handler,
  9593. void *context, int cgroup_fd)
  9594. {
  9595. struct pmu *pmu;
  9596. struct perf_event *event;
  9597. struct hw_perf_event *hwc;
  9598. long err = -EINVAL;
  9599. int node;
  9600. if ((unsigned)cpu >= nr_cpu_ids) {
  9601. if (!task || cpu != -1)
  9602. return ERR_PTR(-EINVAL);
  9603. }
  9604. if (attr->sigtrap && !task) {
  9605. /* Requires a task: avoid signalling random tasks. */
  9606. return ERR_PTR(-EINVAL);
  9607. }
  9608. node = (cpu >= 0) ? cpu_to_node(cpu) : -1;
  9609. event = kmem_cache_alloc_node(perf_event_cache, GFP_KERNEL | __GFP_ZERO,
  9610. node);
  9611. if (!event)
  9612. return ERR_PTR(-ENOMEM);
  9613. /*
  9614. * Single events are their own group leaders, with an
  9615. * empty sibling list:
  9616. */
  9617. if (!group_leader)
  9618. group_leader = event;
  9619. mutex_init(&event->child_mutex);
  9620. INIT_LIST_HEAD(&event->child_list);
  9621. INIT_LIST_HEAD(&event->event_entry);
  9622. INIT_LIST_HEAD(&event->sibling_list);
  9623. INIT_LIST_HEAD(&event->active_list);
  9624. init_event_group(event);
  9625. INIT_LIST_HEAD(&event->rb_entry);
  9626. INIT_LIST_HEAD(&event->active_entry);
  9627. INIT_LIST_HEAD(&event->addr_filters.list);
  9628. INIT_HLIST_NODE(&event->hlist_entry);
  9629. init_waitqueue_head(&event->waitq);
  9630. init_irq_work(&event->pending_irq, perf_pending_irq);
  9631. init_task_work(&event->pending_task, perf_pending_task);
  9632. mutex_init(&event->mmap_mutex);
  9633. raw_spin_lock_init(&event->addr_filters.lock);
  9634. atomic_long_set(&event->refcount, 1);
  9635. event->cpu = cpu;
  9636. event->attr = *attr;
  9637. event->group_leader = group_leader;
  9638. event->pmu = NULL;
  9639. event->oncpu = -1;
  9640. event->parent = parent_event;
  9641. event->ns = get_pid_ns(task_active_pid_ns(current));
  9642. event->id = atomic64_inc_return(&perf_event_id);
  9643. event->state = PERF_EVENT_STATE_INACTIVE;
  9644. if (parent_event)
  9645. event->event_caps = parent_event->event_caps;
  9646. if (task) {
  9647. event->attach_state = PERF_ATTACH_TASK;
  9648. /*
  9649. * XXX pmu::event_init needs to know what task to account to
  9650. * and we cannot use the ctx information because we need the
  9651. * pmu before we get a ctx.
  9652. */
  9653. event->hw.target = get_task_struct(task);
  9654. }
  9655. event->clock = &local_clock;
  9656. if (parent_event)
  9657. event->clock = parent_event->clock;
  9658. if (!overflow_handler && parent_event) {
  9659. overflow_handler = parent_event->overflow_handler;
  9660. context = parent_event->overflow_handler_context;
  9661. #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
  9662. if (overflow_handler == bpf_overflow_handler) {
  9663. struct bpf_prog *prog = parent_event->prog;
  9664. bpf_prog_inc(prog);
  9665. event->prog = prog;
  9666. event->orig_overflow_handler =
  9667. parent_event->orig_overflow_handler;
  9668. }
  9669. #endif
  9670. }
  9671. if (overflow_handler) {
  9672. event->overflow_handler = overflow_handler;
  9673. event->overflow_handler_context = context;
  9674. } else if (is_write_backward(event)){
  9675. event->overflow_handler = perf_event_output_backward;
  9676. event->overflow_handler_context = NULL;
  9677. } else {
  9678. event->overflow_handler = perf_event_output_forward;
  9679. event->overflow_handler_context = NULL;
  9680. }
  9681. perf_event__state_init(event);
  9682. pmu = NULL;
  9683. hwc = &event->hw;
  9684. hwc->sample_period = attr->sample_period;
  9685. if (attr->freq && attr->sample_freq)
  9686. hwc->sample_period = 1;
  9687. hwc->last_period = hwc->sample_period;
  9688. local64_set(&hwc->period_left, hwc->sample_period);
  9689. /*
  9690. * We currently do not support PERF_SAMPLE_READ on inherited events.
  9691. * See perf_output_read().
  9692. */
  9693. if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
  9694. goto err_ns;
  9695. if (!has_branch_stack(event))
  9696. event->attr.branch_sample_type = 0;
  9697. pmu = perf_init_event(event);
  9698. if (IS_ERR(pmu)) {
  9699. err = PTR_ERR(pmu);
  9700. goto err_ns;
  9701. }
  9702. /*
  9703. * Disallow uncore-cgroup events, they don't make sense as the cgroup will
  9704. * be different on other CPUs in the uncore mask.
  9705. */
  9706. if (pmu->task_ctx_nr == perf_invalid_context && cgroup_fd != -1) {
  9707. err = -EINVAL;
  9708. goto err_pmu;
  9709. }
  9710. if (event->attr.aux_output &&
  9711. !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) {
  9712. err = -EOPNOTSUPP;
  9713. goto err_pmu;
  9714. }
  9715. if (cgroup_fd != -1) {
  9716. err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
  9717. if (err)
  9718. goto err_pmu;
  9719. }
  9720. err = exclusive_event_init(event);
  9721. if (err)
  9722. goto err_pmu;
  9723. if (has_addr_filter(event)) {
  9724. event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters,
  9725. sizeof(struct perf_addr_filter_range),
  9726. GFP_KERNEL);
  9727. if (!event->addr_filter_ranges) {
  9728. err = -ENOMEM;
  9729. goto err_per_task;
  9730. }
  9731. /*
  9732. * Clone the parent's vma offsets: they are valid until exec()
  9733. * even if the mm is not shared with the parent.
  9734. */
  9735. if (event->parent) {
  9736. struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
  9737. raw_spin_lock_irq(&ifh->lock);
  9738. memcpy(event->addr_filter_ranges,
  9739. event->parent->addr_filter_ranges,
  9740. pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range));
  9741. raw_spin_unlock_irq(&ifh->lock);
  9742. }
  9743. /* force hw sync on the address filters */
  9744. event->addr_filters_gen = 1;
  9745. }
  9746. if (!event->parent) {
  9747. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  9748. err = get_callchain_buffers(attr->sample_max_stack);
  9749. if (err)
  9750. goto err_addr_filters;
  9751. }
  9752. }
  9753. err = security_perf_event_alloc(event);
  9754. if (err)
  9755. goto err_callchain_buffer;
  9756. /* symmetric to unaccount_event() in _free_event() */
  9757. account_event(event);
  9758. return event;
  9759. err_callchain_buffer:
  9760. if (!event->parent) {
  9761. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  9762. put_callchain_buffers();
  9763. }
  9764. err_addr_filters:
  9765. kfree(event->addr_filter_ranges);
  9766. err_per_task:
  9767. exclusive_event_destroy(event);
  9768. err_pmu:
  9769. if (is_cgroup_event(event))
  9770. perf_detach_cgroup(event);
  9771. if (event->destroy)
  9772. event->destroy(event);
  9773. module_put(pmu->module);
  9774. err_ns:
  9775. if (event->hw.target)
  9776. put_task_struct(event->hw.target);
  9777. call_rcu(&event->rcu_head, free_event_rcu);
  9778. return ERR_PTR(err);
  9779. }
  9780. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  9781. struct perf_event_attr *attr)
  9782. {
  9783. u32 size;
  9784. int ret;
  9785. /* Zero the full structure, so that a short copy will be nice. */
  9786. memset(attr, 0, sizeof(*attr));
  9787. ret = get_user(size, &uattr->size);
  9788. if (ret)
  9789. return ret;
  9790. /* ABI compatibility quirk: */
  9791. if (!size)
  9792. size = PERF_ATTR_SIZE_VER0;
  9793. if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE)
  9794. goto err_size;
  9795. ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
  9796. if (ret) {
  9797. if (ret == -E2BIG)
  9798. goto err_size;
  9799. return ret;
  9800. }
  9801. attr->size = size;
  9802. if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
  9803. return -EINVAL;
  9804. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  9805. return -EINVAL;
  9806. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  9807. return -EINVAL;
  9808. if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
  9809. u64 mask = attr->branch_sample_type;
  9810. /* only using defined bits */
  9811. if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
  9812. return -EINVAL;
  9813. /* at least one branch bit must be set */
  9814. if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
  9815. return -EINVAL;
  9816. /* propagate priv level, when not set for branch */
  9817. if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
  9818. /* exclude_kernel checked on syscall entry */
  9819. if (!attr->exclude_kernel)
  9820. mask |= PERF_SAMPLE_BRANCH_KERNEL;
  9821. if (!attr->exclude_user)
  9822. mask |= PERF_SAMPLE_BRANCH_USER;
  9823. if (!attr->exclude_hv)
  9824. mask |= PERF_SAMPLE_BRANCH_HV;
  9825. /*
  9826. * adjust user setting (for HW filter setup)
  9827. */
  9828. attr->branch_sample_type = mask;
  9829. }
  9830. /* privileged levels capture (kernel, hv): check permissions */
  9831. if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) {
  9832. ret = perf_allow_kernel(attr);
  9833. if (ret)
  9834. return ret;
  9835. }
  9836. }
  9837. if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
  9838. ret = perf_reg_validate(attr->sample_regs_user);
  9839. if (ret)
  9840. return ret;
  9841. }
  9842. if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
  9843. if (!arch_perf_have_user_stack_dump())
  9844. return -ENOSYS;
  9845. /*
  9846. * We have __u32 type for the size, but so far
  9847. * we can only use __u16 as maximum due to the
  9848. * __u16 sample size limit.
  9849. */
  9850. if (attr->sample_stack_user >= USHRT_MAX)
  9851. return -EINVAL;
  9852. else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
  9853. return -EINVAL;
  9854. }
  9855. if (!attr->sample_max_stack)
  9856. attr->sample_max_stack = sysctl_perf_event_max_stack;
  9857. if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
  9858. ret = perf_reg_validate(attr->sample_regs_intr);
  9859. #ifndef CONFIG_CGROUP_PERF
  9860. if (attr->sample_type & PERF_SAMPLE_CGROUP)
  9861. return -EINVAL;
  9862. #endif
  9863. if ((attr->sample_type & PERF_SAMPLE_WEIGHT) &&
  9864. (attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT))
  9865. return -EINVAL;
  9866. if (!attr->inherit && attr->inherit_thread)
  9867. return -EINVAL;
  9868. if (attr->remove_on_exec && attr->enable_on_exec)
  9869. return -EINVAL;
  9870. if (attr->sigtrap && !attr->remove_on_exec)
  9871. return -EINVAL;
  9872. out:
  9873. return ret;
  9874. err_size:
  9875. put_user(sizeof(*attr), &uattr->size);
  9876. ret = -E2BIG;
  9877. goto out;
  9878. }
  9879. static void mutex_lock_double(struct mutex *a, struct mutex *b)
  9880. {
  9881. if (b < a)
  9882. swap(a, b);
  9883. mutex_lock(a);
  9884. mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
  9885. }
  9886. static int
  9887. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  9888. {
  9889. struct perf_buffer *rb = NULL;
  9890. int ret = -EINVAL;
  9891. if (!output_event) {
  9892. mutex_lock(&event->mmap_mutex);
  9893. goto set;
  9894. }
  9895. /* don't allow circular references */
  9896. if (event == output_event)
  9897. goto out;
  9898. /*
  9899. * Don't allow cross-cpu buffers
  9900. */
  9901. if (output_event->cpu != event->cpu)
  9902. goto out;
  9903. /*
  9904. * If its not a per-cpu rb, it must be the same task.
  9905. */
  9906. if (output_event->cpu == -1 && output_event->hw.target != event->hw.target)
  9907. goto out;
  9908. /*
  9909. * Mixing clocks in the same buffer is trouble you don't need.
  9910. */
  9911. if (output_event->clock != event->clock)
  9912. goto out;
  9913. /*
  9914. * Either writing ring buffer from beginning or from end.
  9915. * Mixing is not allowed.
  9916. */
  9917. if (is_write_backward(output_event) != is_write_backward(event))
  9918. goto out;
  9919. /*
  9920. * If both events generate aux data, they must be on the same PMU
  9921. */
  9922. if (has_aux(event) && has_aux(output_event) &&
  9923. event->pmu != output_event->pmu)
  9924. goto out;
  9925. /*
  9926. * Hold both mmap_mutex to serialize against perf_mmap_close(). Since
  9927. * output_event is already on rb->event_list, and the list iteration
  9928. * restarts after every removal, it is guaranteed this new event is
  9929. * observed *OR* if output_event is already removed, it's guaranteed we
  9930. * observe !rb->mmap_count.
  9931. */
  9932. mutex_lock_double(&event->mmap_mutex, &output_event->mmap_mutex);
  9933. set:
  9934. /* Can't redirect output if we've got an active mmap() */
  9935. if (atomic_read(&event->mmap_count))
  9936. goto unlock;
  9937. if (output_event) {
  9938. /* get the rb we want to redirect to */
  9939. rb = ring_buffer_get(output_event);
  9940. if (!rb)
  9941. goto unlock;
  9942. /* did we race against perf_mmap_close() */
  9943. if (!atomic_read(&rb->mmap_count)) {
  9944. ring_buffer_put(rb);
  9945. goto unlock;
  9946. }
  9947. }
  9948. ring_buffer_attach(event, rb);
  9949. ret = 0;
  9950. unlock:
  9951. mutex_unlock(&event->mmap_mutex);
  9952. if (output_event)
  9953. mutex_unlock(&output_event->mmap_mutex);
  9954. out:
  9955. return ret;
  9956. }
  9957. static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
  9958. {
  9959. bool nmi_safe = false;
  9960. switch (clk_id) {
  9961. case CLOCK_MONOTONIC:
  9962. event->clock = &ktime_get_mono_fast_ns;
  9963. nmi_safe = true;
  9964. break;
  9965. case CLOCK_MONOTONIC_RAW:
  9966. event->clock = &ktime_get_raw_fast_ns;
  9967. nmi_safe = true;
  9968. break;
  9969. case CLOCK_REALTIME:
  9970. event->clock = &ktime_get_real_ns;
  9971. break;
  9972. case CLOCK_BOOTTIME:
  9973. event->clock = &ktime_get_boottime_ns;
  9974. break;
  9975. case CLOCK_TAI:
  9976. event->clock = &ktime_get_clocktai_ns;
  9977. break;
  9978. default:
  9979. return -EINVAL;
  9980. }
  9981. if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
  9982. return -EINVAL;
  9983. return 0;
  9984. }
  9985. /*
  9986. * Variation on perf_event_ctx_lock_nested(), except we take two context
  9987. * mutexes.
  9988. */
  9989. static struct perf_event_context *
  9990. __perf_event_ctx_lock_double(struct perf_event *group_leader,
  9991. struct perf_event_context *ctx)
  9992. {
  9993. struct perf_event_context *gctx;
  9994. again:
  9995. rcu_read_lock();
  9996. gctx = READ_ONCE(group_leader->ctx);
  9997. if (!refcount_inc_not_zero(&gctx->refcount)) {
  9998. rcu_read_unlock();
  9999. goto again;
  10000. }
  10001. rcu_read_unlock();
  10002. mutex_lock_double(&gctx->mutex, &ctx->mutex);
  10003. if (group_leader->ctx != gctx) {
  10004. mutex_unlock(&ctx->mutex);
  10005. mutex_unlock(&gctx->mutex);
  10006. put_ctx(gctx);
  10007. goto again;
  10008. }
  10009. return gctx;
  10010. }
  10011. static bool
  10012. perf_check_permission(struct perf_event_attr *attr, struct task_struct *task)
  10013. {
  10014. unsigned int ptrace_mode = PTRACE_MODE_READ_REALCREDS;
  10015. bool is_capable = perfmon_capable();
  10016. if (attr->sigtrap) {
  10017. /*
  10018. * perf_event_attr::sigtrap sends signals to the other task.
  10019. * Require the current task to also have CAP_KILL.
  10020. */
  10021. rcu_read_lock();
  10022. is_capable &= ns_capable(__task_cred(task)->user_ns, CAP_KILL);
  10023. rcu_read_unlock();
  10024. /*
  10025. * If the required capabilities aren't available, checks for
  10026. * ptrace permissions: upgrade to ATTACH, since sending signals
  10027. * can effectively change the target task.
  10028. */
  10029. ptrace_mode = PTRACE_MODE_ATTACH_REALCREDS;
  10030. }
  10031. /*
  10032. * Preserve ptrace permission check for backwards compatibility. The
  10033. * ptrace check also includes checks that the current task and other
  10034. * task have matching uids, and is therefore not done here explicitly.
  10035. */
  10036. return is_capable || ptrace_may_access(task, ptrace_mode);
  10037. }
  10038. /**
  10039. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  10040. *
  10041. * @attr_uptr: event_id type attributes for monitoring/sampling
  10042. * @pid: target pid
  10043. * @cpu: target cpu
  10044. * @group_fd: group leader event fd
  10045. * @flags: perf event open flags
  10046. */
  10047. SYSCALL_DEFINE5(perf_event_open,
  10048. struct perf_event_attr __user *, attr_uptr,
  10049. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  10050. {
  10051. struct perf_event *group_leader = NULL, *output_event = NULL;
  10052. struct perf_event *event, *sibling;
  10053. struct perf_event_attr attr;
  10054. struct perf_event_context *ctx, *gctx;
  10055. struct file *event_file = NULL;
  10056. struct fd group = {NULL, 0};
  10057. struct task_struct *task = NULL;
  10058. struct pmu *pmu;
  10059. int event_fd;
  10060. int move_group = 0;
  10061. int err;
  10062. int f_flags = O_RDWR;
  10063. int cgroup_fd = -1;
  10064. /* for future expandability... */
  10065. if (flags & ~PERF_FLAG_ALL)
  10066. return -EINVAL;
  10067. err = perf_copy_attr(attr_uptr, &attr);
  10068. if (err)
  10069. return err;
  10070. /* Do we allow access to perf_event_open(2) ? */
  10071. err = security_perf_event_open(&attr, PERF_SECURITY_OPEN);
  10072. if (err)
  10073. return err;
  10074. if (!attr.exclude_kernel) {
  10075. err = perf_allow_kernel(&attr);
  10076. if (err)
  10077. return err;
  10078. }
  10079. if (attr.namespaces) {
  10080. if (!perfmon_capable())
  10081. return -EACCES;
  10082. }
  10083. if (attr.freq) {
  10084. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  10085. return -EINVAL;
  10086. } else {
  10087. if (attr.sample_period & (1ULL << 63))
  10088. return -EINVAL;
  10089. }
  10090. /* Only privileged users can get physical addresses */
  10091. if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) {
  10092. err = perf_allow_kernel(&attr);
  10093. if (err)
  10094. return err;
  10095. }
  10096. /* REGS_INTR can leak data, lockdown must prevent this */
  10097. if (attr.sample_type & PERF_SAMPLE_REGS_INTR) {
  10098. err = security_locked_down(LOCKDOWN_PERF);
  10099. if (err)
  10100. return err;
  10101. }
  10102. /*
  10103. * In cgroup mode, the pid argument is used to pass the fd
  10104. * opened to the cgroup directory in cgroupfs. The cpu argument
  10105. * designates the cpu on which to monitor threads from that
  10106. * cgroup.
  10107. */
  10108. if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
  10109. return -EINVAL;
  10110. if (flags & PERF_FLAG_FD_CLOEXEC)
  10111. f_flags |= O_CLOEXEC;
  10112. event_fd = get_unused_fd_flags(f_flags);
  10113. if (event_fd < 0)
  10114. return event_fd;
  10115. if (group_fd != -1) {
  10116. err = perf_fget_light(group_fd, &group);
  10117. if (err)
  10118. goto err_fd;
  10119. group_leader = group.file->private_data;
  10120. if (flags & PERF_FLAG_FD_OUTPUT)
  10121. output_event = group_leader;
  10122. if (flags & PERF_FLAG_FD_NO_GROUP)
  10123. group_leader = NULL;
  10124. }
  10125. if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
  10126. task = find_lively_task_by_vpid(pid);
  10127. if (IS_ERR(task)) {
  10128. err = PTR_ERR(task);
  10129. goto err_group_fd;
  10130. }
  10131. }
  10132. if (task && group_leader &&
  10133. group_leader->attr.inherit != attr.inherit) {
  10134. err = -EINVAL;
  10135. goto err_task;
  10136. }
  10137. if (flags & PERF_FLAG_PID_CGROUP)
  10138. cgroup_fd = pid;
  10139. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
  10140. NULL, NULL, cgroup_fd);
  10141. if (IS_ERR(event)) {
  10142. err = PTR_ERR(event);
  10143. goto err_task;
  10144. }
  10145. if (is_sampling_event(event)) {
  10146. if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
  10147. err = -EOPNOTSUPP;
  10148. goto err_alloc;
  10149. }
  10150. }
  10151. /*
  10152. * Special case software events and allow them to be part of
  10153. * any hardware group.
  10154. */
  10155. pmu = event->pmu;
  10156. if (attr.use_clockid) {
  10157. err = perf_event_set_clock(event, attr.clockid);
  10158. if (err)
  10159. goto err_alloc;
  10160. }
  10161. if (pmu->task_ctx_nr == perf_sw_context)
  10162. event->event_caps |= PERF_EV_CAP_SOFTWARE;
  10163. if (group_leader) {
  10164. if (is_software_event(event) &&
  10165. !in_software_context(group_leader)) {
  10166. /*
  10167. * If the event is a sw event, but the group_leader
  10168. * is on hw context.
  10169. *
  10170. * Allow the addition of software events to hw
  10171. * groups, this is safe because software events
  10172. * never fail to schedule.
  10173. */
  10174. pmu = group_leader->ctx->pmu;
  10175. } else if (!is_software_event(event) &&
  10176. is_software_event(group_leader) &&
  10177. (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
  10178. /*
  10179. * In case the group is a pure software group, and we
  10180. * try to add a hardware event, move the whole group to
  10181. * the hardware context.
  10182. */
  10183. move_group = 1;
  10184. }
  10185. }
  10186. /*
  10187. * Get the target context (task or percpu):
  10188. */
  10189. ctx = find_get_context(pmu, task, event);
  10190. if (IS_ERR(ctx)) {
  10191. err = PTR_ERR(ctx);
  10192. goto err_alloc;
  10193. }
  10194. /*
  10195. * Look up the group leader (we will attach this event to it):
  10196. */
  10197. if (group_leader) {
  10198. err = -EINVAL;
  10199. /*
  10200. * Do not allow a recursive hierarchy (this new sibling
  10201. * becoming part of another group-sibling):
  10202. */
  10203. if (group_leader->group_leader != group_leader)
  10204. goto err_context;
  10205. /* All events in a group should have the same clock */
  10206. if (group_leader->clock != event->clock)
  10207. goto err_context;
  10208. /*
  10209. * Make sure we're both events for the same CPU;
  10210. * grouping events for different CPUs is broken; since
  10211. * you can never concurrently schedule them anyhow.
  10212. */
  10213. if (group_leader->cpu != event->cpu)
  10214. goto err_context;
  10215. /*
  10216. * Make sure we're both on the same task, or both
  10217. * per-CPU events.
  10218. */
  10219. if (group_leader->ctx->task != ctx->task)
  10220. goto err_context;
  10221. /*
  10222. * Do not allow to attach to a group in a different task
  10223. * or CPU context. If we're moving SW events, we'll fix
  10224. * this up later, so allow that.
  10225. *
  10226. * Racy, not holding group_leader->ctx->mutex, see comment with
  10227. * perf_event_ctx_lock().
  10228. */
  10229. if (!move_group && group_leader->ctx != ctx)
  10230. goto err_context;
  10231. /*
  10232. * Only a group leader can be exclusive or pinned
  10233. */
  10234. if (attr.exclusive || attr.pinned)
  10235. goto err_context;
  10236. }
  10237. if (output_event) {
  10238. err = perf_event_set_output(event, output_event);
  10239. if (err)
  10240. goto err_context;
  10241. }
  10242. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
  10243. f_flags);
  10244. if (IS_ERR(event_file)) {
  10245. err = PTR_ERR(event_file);
  10246. event_file = NULL;
  10247. goto err_context;
  10248. }
  10249. if (task) {
  10250. err = down_read_interruptible(&task->signal->exec_update_lock);
  10251. if (err)
  10252. goto err_file;
  10253. /*
  10254. * We must hold exec_update_lock across this and any potential
  10255. * perf_install_in_context() call for this new event to
  10256. * serialize against exec() altering our credentials (and the
  10257. * perf_event_exit_task() that could imply).
  10258. */
  10259. err = -EACCES;
  10260. if (!perf_check_permission(&attr, task))
  10261. goto err_cred;
  10262. }
  10263. if (move_group) {
  10264. gctx = __perf_event_ctx_lock_double(group_leader, ctx);
  10265. if (gctx->task == TASK_TOMBSTONE) {
  10266. err = -ESRCH;
  10267. goto err_locked;
  10268. }
  10269. /*
  10270. * Check if we raced against another sys_perf_event_open() call
  10271. * moving the software group underneath us.
  10272. */
  10273. if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
  10274. /*
  10275. * If someone moved the group out from under us, check
  10276. * if this new event wound up on the same ctx, if so
  10277. * its the regular !move_group case, otherwise fail.
  10278. */
  10279. if (gctx != ctx) {
  10280. err = -EINVAL;
  10281. goto err_locked;
  10282. } else {
  10283. perf_event_ctx_unlock(group_leader, gctx);
  10284. move_group = 0;
  10285. goto not_move_group;
  10286. }
  10287. }
  10288. /*
  10289. * Failure to create exclusive events returns -EBUSY.
  10290. */
  10291. err = -EBUSY;
  10292. if (!exclusive_event_installable(group_leader, ctx))
  10293. goto err_locked;
  10294. for_each_sibling_event(sibling, group_leader) {
  10295. if (!exclusive_event_installable(sibling, ctx))
  10296. goto err_locked;
  10297. }
  10298. } else {
  10299. mutex_lock(&ctx->mutex);
  10300. /*
  10301. * Now that we hold ctx->lock, (re)validate group_leader->ctx == ctx,
  10302. * see the group_leader && !move_group test earlier.
  10303. */
  10304. if (group_leader && group_leader->ctx != ctx) {
  10305. err = -EINVAL;
  10306. goto err_locked;
  10307. }
  10308. }
  10309. not_move_group:
  10310. if (ctx->task == TASK_TOMBSTONE) {
  10311. err = -ESRCH;
  10312. goto err_locked;
  10313. }
  10314. if (!perf_event_validate_size(event)) {
  10315. err = -E2BIG;
  10316. goto err_locked;
  10317. }
  10318. if (!task) {
  10319. /*
  10320. * Check if the @cpu we're creating an event for is online.
  10321. *
  10322. * We use the perf_cpu_context::ctx::mutex to serialize against
  10323. * the hotplug notifiers. See perf_event_{init,exit}_cpu().
  10324. */
  10325. struct perf_cpu_context *cpuctx =
  10326. container_of(ctx, struct perf_cpu_context, ctx);
  10327. if (!cpuctx->online) {
  10328. err = -ENODEV;
  10329. goto err_locked;
  10330. }
  10331. }
  10332. if (perf_need_aux_event(event) && !perf_get_aux_event(event, group_leader)) {
  10333. err = -EINVAL;
  10334. goto err_locked;
  10335. }
  10336. /*
  10337. * Must be under the same ctx::mutex as perf_install_in_context(),
  10338. * because we need to serialize with concurrent event creation.
  10339. */
  10340. if (!exclusive_event_installable(event, ctx)) {
  10341. err = -EBUSY;
  10342. goto err_locked;
  10343. }
  10344. WARN_ON_ONCE(ctx->parent_ctx);
  10345. /*
  10346. * This is the point on no return; we cannot fail hereafter. This is
  10347. * where we start modifying current state.
  10348. */
  10349. if (move_group) {
  10350. /*
  10351. * See perf_event_ctx_lock() for comments on the details
  10352. * of swizzling perf_event::ctx.
  10353. */
  10354. perf_remove_from_context(group_leader, 0);
  10355. put_ctx(gctx);
  10356. for_each_sibling_event(sibling, group_leader) {
  10357. perf_remove_from_context(sibling, 0);
  10358. put_ctx(gctx);
  10359. }
  10360. /*
  10361. * Wait for everybody to stop referencing the events through
  10362. * the old lists, before installing it on new lists.
  10363. */
  10364. synchronize_rcu();
  10365. /*
  10366. * Install the group siblings before the group leader.
  10367. *
  10368. * Because a group leader will try and install the entire group
  10369. * (through the sibling list, which is still in-tact), we can
  10370. * end up with siblings installed in the wrong context.
  10371. *
  10372. * By installing siblings first we NO-OP because they're not
  10373. * reachable through the group lists.
  10374. */
  10375. for_each_sibling_event(sibling, group_leader) {
  10376. perf_event__state_init(sibling);
  10377. perf_install_in_context(ctx, sibling, sibling->cpu);
  10378. get_ctx(ctx);
  10379. }
  10380. /*
  10381. * Removing from the context ends up with disabled
  10382. * event. What we want here is event in the initial
  10383. * startup state, ready to be add into new context.
  10384. */
  10385. perf_event__state_init(group_leader);
  10386. perf_install_in_context(ctx, group_leader, group_leader->cpu);
  10387. get_ctx(ctx);
  10388. }
  10389. /*
  10390. * Precalculate sample_data sizes; do while holding ctx::mutex such
  10391. * that we're serialized against further additions and before
  10392. * perf_install_in_context() which is the point the event is active and
  10393. * can use these values.
  10394. */
  10395. perf_event__header_size(event);
  10396. perf_event__id_header_size(event);
  10397. event->owner = current;
  10398. perf_install_in_context(ctx, event, event->cpu);
  10399. perf_unpin_context(ctx);
  10400. if (move_group)
  10401. perf_event_ctx_unlock(group_leader, gctx);
  10402. mutex_unlock(&ctx->mutex);
  10403. if (task) {
  10404. up_read(&task->signal->exec_update_lock);
  10405. put_task_struct(task);
  10406. }
  10407. mutex_lock(&current->perf_event_mutex);
  10408. list_add_tail(&event->owner_entry, &current->perf_event_list);
  10409. mutex_unlock(&current->perf_event_mutex);
  10410. /*
  10411. * Drop the reference on the group_event after placing the
  10412. * new event on the sibling_list. This ensures destruction
  10413. * of the group leader will find the pointer to itself in
  10414. * perf_group_detach().
  10415. */
  10416. fdput(group);
  10417. fd_install(event_fd, event_file);
  10418. return event_fd;
  10419. err_locked:
  10420. if (move_group)
  10421. perf_event_ctx_unlock(group_leader, gctx);
  10422. mutex_unlock(&ctx->mutex);
  10423. err_cred:
  10424. if (task)
  10425. up_read(&task->signal->exec_update_lock);
  10426. err_file:
  10427. fput(event_file);
  10428. err_context:
  10429. perf_unpin_context(ctx);
  10430. put_ctx(ctx);
  10431. err_alloc:
  10432. /*
  10433. * If event_file is set, the fput() above will have called ->release()
  10434. * and that will take care of freeing the event.
  10435. */
  10436. if (!event_file)
  10437. free_event(event);
  10438. err_task:
  10439. if (task)
  10440. put_task_struct(task);
  10441. err_group_fd:
  10442. fdput(group);
  10443. err_fd:
  10444. put_unused_fd(event_fd);
  10445. return err;
  10446. }
  10447. /**
  10448. * perf_event_create_kernel_counter
  10449. *
  10450. * @attr: attributes of the counter to create
  10451. * @cpu: cpu in which the counter is bound
  10452. * @task: task to profile (NULL for percpu)
  10453. * @overflow_handler: callback to trigger when we hit the event
  10454. * @context: context data could be used in overflow_handler callback
  10455. */
  10456. struct perf_event *
  10457. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  10458. struct task_struct *task,
  10459. perf_overflow_handler_t overflow_handler,
  10460. void *context)
  10461. {
  10462. struct perf_event_context *ctx;
  10463. struct perf_event *event;
  10464. int err;
  10465. /*
  10466. * Grouping is not supported for kernel events, neither is 'AUX',
  10467. * make sure the caller's intentions are adjusted.
  10468. */
  10469. if (attr->aux_output)
  10470. return ERR_PTR(-EINVAL);
  10471. event = perf_event_alloc(attr, cpu, task, NULL, NULL,
  10472. overflow_handler, context, -1);
  10473. if (IS_ERR(event)) {
  10474. err = PTR_ERR(event);
  10475. goto err;
  10476. }
  10477. /* Mark owner so we could distinguish it from user events. */
  10478. event->owner = TASK_TOMBSTONE;
  10479. /*
  10480. * Get the target context (task or percpu):
  10481. */
  10482. ctx = find_get_context(event->pmu, task, event);
  10483. if (IS_ERR(ctx)) {
  10484. err = PTR_ERR(ctx);
  10485. goto err_free;
  10486. }
  10487. WARN_ON_ONCE(ctx->parent_ctx);
  10488. mutex_lock(&ctx->mutex);
  10489. if (ctx->task == TASK_TOMBSTONE) {
  10490. err = -ESRCH;
  10491. goto err_unlock;
  10492. }
  10493. if (!task) {
  10494. /*
  10495. * Check if the @cpu we're creating an event for is online.
  10496. *
  10497. * We use the perf_cpu_context::ctx::mutex to serialize against
  10498. * the hotplug notifiers. See perf_event_{init,exit}_cpu().
  10499. */
  10500. struct perf_cpu_context *cpuctx =
  10501. container_of(ctx, struct perf_cpu_context, ctx);
  10502. if (!cpuctx->online) {
  10503. err = -ENODEV;
  10504. goto err_unlock;
  10505. }
  10506. }
  10507. if (!exclusive_event_installable(event, ctx)) {
  10508. err = -EBUSY;
  10509. goto err_unlock;
  10510. }
  10511. perf_install_in_context(ctx, event, event->cpu);
  10512. perf_unpin_context(ctx);
  10513. mutex_unlock(&ctx->mutex);
  10514. return event;
  10515. err_unlock:
  10516. mutex_unlock(&ctx->mutex);
  10517. perf_unpin_context(ctx);
  10518. put_ctx(ctx);
  10519. err_free:
  10520. free_event(event);
  10521. err:
  10522. return ERR_PTR(err);
  10523. }
  10524. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  10525. void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
  10526. {
  10527. struct perf_event_context *src_ctx;
  10528. struct perf_event_context *dst_ctx;
  10529. struct perf_event *event, *tmp;
  10530. LIST_HEAD(events);
  10531. src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
  10532. dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
  10533. /*
  10534. * See perf_event_ctx_lock() for comments on the details
  10535. * of swizzling perf_event::ctx.
  10536. */
  10537. mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
  10538. list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
  10539. event_entry) {
  10540. perf_remove_from_context(event, 0);
  10541. unaccount_event_cpu(event, src_cpu);
  10542. put_ctx(src_ctx);
  10543. list_add(&event->migrate_entry, &events);
  10544. }
  10545. /*
  10546. * Wait for the events to quiesce before re-instating them.
  10547. */
  10548. synchronize_rcu();
  10549. /*
  10550. * Re-instate events in 2 passes.
  10551. *
  10552. * Skip over group leaders and only install siblings on this first
  10553. * pass, siblings will not get enabled without a leader, however a
  10554. * leader will enable its siblings, even if those are still on the old
  10555. * context.
  10556. */
  10557. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  10558. if (event->group_leader == event)
  10559. continue;
  10560. list_del(&event->migrate_entry);
  10561. if (event->state >= PERF_EVENT_STATE_OFF)
  10562. event->state = PERF_EVENT_STATE_INACTIVE;
  10563. account_event_cpu(event, dst_cpu);
  10564. perf_install_in_context(dst_ctx, event, dst_cpu);
  10565. get_ctx(dst_ctx);
  10566. }
  10567. /*
  10568. * Once all the siblings are setup properly, install the group leaders
  10569. * to make it go.
  10570. */
  10571. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  10572. list_del(&event->migrate_entry);
  10573. if (event->state >= PERF_EVENT_STATE_OFF)
  10574. event->state = PERF_EVENT_STATE_INACTIVE;
  10575. account_event_cpu(event, dst_cpu);
  10576. perf_install_in_context(dst_ctx, event, dst_cpu);
  10577. get_ctx(dst_ctx);
  10578. }
  10579. mutex_unlock(&dst_ctx->mutex);
  10580. mutex_unlock(&src_ctx->mutex);
  10581. }
  10582. EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
  10583. static void sync_child_event(struct perf_event *child_event)
  10584. {
  10585. struct perf_event *parent_event = child_event->parent;
  10586. u64 child_val;
  10587. if (child_event->attr.inherit_stat) {
  10588. struct task_struct *task = child_event->ctx->task;
  10589. if (task && task != TASK_TOMBSTONE)
  10590. perf_event_read_event(child_event, task);
  10591. }
  10592. child_val = perf_event_count(child_event);
  10593. /*
  10594. * Add back the child's count to the parent's count:
  10595. */
  10596. atomic64_add(child_val, &parent_event->child_count);
  10597. atomic64_add(child_event->total_time_enabled,
  10598. &parent_event->child_total_time_enabled);
  10599. atomic64_add(child_event->total_time_running,
  10600. &parent_event->child_total_time_running);
  10601. }
  10602. static void
  10603. perf_event_exit_event(struct perf_event *event, struct perf_event_context *ctx)
  10604. {
  10605. struct perf_event *parent_event = event->parent;
  10606. unsigned long detach_flags = 0;
  10607. if (parent_event) {
  10608. /*
  10609. * Do not destroy the 'original' grouping; because of the
  10610. * context switch optimization the original events could've
  10611. * ended up in a random child task.
  10612. *
  10613. * If we were to destroy the original group, all group related
  10614. * operations would cease to function properly after this
  10615. * random child dies.
  10616. *
  10617. * Do destroy all inherited groups, we don't care about those
  10618. * and being thorough is better.
  10619. */
  10620. detach_flags = DETACH_GROUP | DETACH_CHILD;
  10621. mutex_lock(&parent_event->child_mutex);
  10622. }
  10623. perf_remove_from_context(event, detach_flags);
  10624. raw_spin_lock_irq(&ctx->lock);
  10625. if (event->state > PERF_EVENT_STATE_EXIT)
  10626. perf_event_set_state(event, PERF_EVENT_STATE_EXIT);
  10627. raw_spin_unlock_irq(&ctx->lock);
  10628. /*
  10629. * Child events can be freed.
  10630. */
  10631. if (parent_event) {
  10632. mutex_unlock(&parent_event->child_mutex);
  10633. /*
  10634. * Kick perf_poll() for is_event_hup();
  10635. */
  10636. perf_event_wakeup(parent_event);
  10637. free_event(event);
  10638. put_event(parent_event);
  10639. return;
  10640. }
  10641. /*
  10642. * Parent events are governed by their filedesc, retain them.
  10643. */
  10644. perf_event_wakeup(event);
  10645. }
  10646. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  10647. {
  10648. struct perf_event_context *child_ctx, *clone_ctx = NULL;
  10649. struct perf_event *child_event, *next;
  10650. WARN_ON_ONCE(child != current);
  10651. child_ctx = perf_pin_task_context(child, ctxn);
  10652. if (!child_ctx)
  10653. return;
  10654. /*
  10655. * In order to reduce the amount of tricky in ctx tear-down, we hold
  10656. * ctx::mutex over the entire thing. This serializes against almost
  10657. * everything that wants to access the ctx.
  10658. *
  10659. * The exception is sys_perf_event_open() /
  10660. * perf_event_create_kernel_count() which does find_get_context()
  10661. * without ctx::mutex (it cannot because of the move_group double mutex
  10662. * lock thing). See the comments in perf_install_in_context().
  10663. */
  10664. mutex_lock(&child_ctx->mutex);
  10665. /*
  10666. * In a single ctx::lock section, de-schedule the events and detach the
  10667. * context from the task such that we cannot ever get it scheduled back
  10668. * in.
  10669. */
  10670. raw_spin_lock_irq(&child_ctx->lock);
  10671. task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx, EVENT_ALL);
  10672. /*
  10673. * Now that the context is inactive, destroy the task <-> ctx relation
  10674. * and mark the context dead.
  10675. */
  10676. RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
  10677. put_ctx(child_ctx); /* cannot be last */
  10678. WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
  10679. put_task_struct(current); /* cannot be last */
  10680. clone_ctx = unclone_ctx(child_ctx);
  10681. raw_spin_unlock_irq(&child_ctx->lock);
  10682. if (clone_ctx)
  10683. put_ctx(clone_ctx);
  10684. /*
  10685. * Report the task dead after unscheduling the events so that we
  10686. * won't get any samples after PERF_RECORD_EXIT. We can however still
  10687. * get a few PERF_RECORD_READ events.
  10688. */
  10689. perf_event_task(child, child_ctx, 0);
  10690. list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
  10691. perf_event_exit_event(child_event, child_ctx);
  10692. mutex_unlock(&child_ctx->mutex);
  10693. put_ctx(child_ctx);
  10694. }
  10695. /*
  10696. * When a child task exits, feed back event values to parent events.
  10697. *
  10698. * Can be called with exec_update_lock held when called from
  10699. * setup_new_exec().
  10700. */
  10701. void perf_event_exit_task(struct task_struct *child)
  10702. {
  10703. struct perf_event *event, *tmp;
  10704. int ctxn;
  10705. mutex_lock(&child->perf_event_mutex);
  10706. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  10707. owner_entry) {
  10708. list_del_init(&event->owner_entry);
  10709. /*
  10710. * Ensure the list deletion is visible before we clear
  10711. * the owner, closes a race against perf_release() where
  10712. * we need to serialize on the owner->perf_event_mutex.
  10713. */
  10714. smp_store_release(&event->owner, NULL);
  10715. }
  10716. mutex_unlock(&child->perf_event_mutex);
  10717. for_each_task_context_nr(ctxn)
  10718. perf_event_exit_task_context(child, ctxn);
  10719. /*
  10720. * The perf_event_exit_task_context calls perf_event_task
  10721. * with child's task_ctx, which generates EXIT events for
  10722. * child contexts and sets child->perf_event_ctxp[] to NULL.
  10723. * At this point we need to send EXIT events to cpu contexts.
  10724. */
  10725. perf_event_task(child, NULL, 0);
  10726. }
  10727. static void perf_free_event(struct perf_event *event,
  10728. struct perf_event_context *ctx)
  10729. {
  10730. struct perf_event *parent = event->parent;
  10731. if (WARN_ON_ONCE(!parent))
  10732. return;
  10733. mutex_lock(&parent->child_mutex);
  10734. list_del_init(&event->child_list);
  10735. mutex_unlock(&parent->child_mutex);
  10736. put_event(parent);
  10737. raw_spin_lock_irq(&ctx->lock);
  10738. perf_group_detach(event);
  10739. list_del_event(event, ctx);
  10740. raw_spin_unlock_irq(&ctx->lock);
  10741. free_event(event);
  10742. }
  10743. /*
  10744. * Free a context as created by inheritance by perf_event_init_task() below,
  10745. * used by fork() in case of fail.
  10746. *
  10747. * Even though the task has never lived, the context and events have been
  10748. * exposed through the child_list, so we must take care tearing it all down.
  10749. */
  10750. void perf_event_free_task(struct task_struct *task)
  10751. {
  10752. struct perf_event_context *ctx;
  10753. struct perf_event *event, *tmp;
  10754. int ctxn;
  10755. for_each_task_context_nr(ctxn) {
  10756. ctx = task->perf_event_ctxp[ctxn];
  10757. if (!ctx)
  10758. continue;
  10759. mutex_lock(&ctx->mutex);
  10760. raw_spin_lock_irq(&ctx->lock);
  10761. /*
  10762. * Destroy the task <-> ctx relation and mark the context dead.
  10763. *
  10764. * This is important because even though the task hasn't been
  10765. * exposed yet the context has been (through child_list).
  10766. */
  10767. RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
  10768. WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
  10769. put_task_struct(task); /* cannot be last */
  10770. raw_spin_unlock_irq(&ctx->lock);
  10771. list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry)
  10772. perf_free_event(event, ctx);
  10773. mutex_unlock(&ctx->mutex);
  10774. /*
  10775. * perf_event_release_kernel() could've stolen some of our
  10776. * child events and still have them on its free_list. In that
  10777. * case we must wait for these events to have been freed (in
  10778. * particular all their references to this task must've been
  10779. * dropped).
  10780. *
  10781. * Without this copy_process() will unconditionally free this
  10782. * task (irrespective of its reference count) and
  10783. * _free_event()'s put_task_struct(event->hw.target) will be a
  10784. * use-after-free.
  10785. *
  10786. * Wait for all events to drop their context reference.
  10787. */
  10788. wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1);
  10789. put_ctx(ctx); /* must be last */
  10790. }
  10791. }
  10792. void perf_event_delayed_put(struct task_struct *task)
  10793. {
  10794. int ctxn;
  10795. for_each_task_context_nr(ctxn)
  10796. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  10797. }
  10798. struct file *perf_event_get(unsigned int fd)
  10799. {
  10800. struct file *file = fget(fd);
  10801. if (!file)
  10802. return ERR_PTR(-EBADF);
  10803. if (file->f_op != &perf_fops) {
  10804. fput(file);
  10805. return ERR_PTR(-EBADF);
  10806. }
  10807. return file;
  10808. }
  10809. const struct perf_event *perf_get_event(struct file *file)
  10810. {
  10811. if (file->f_op != &perf_fops)
  10812. return ERR_PTR(-EINVAL);
  10813. return file->private_data;
  10814. }
  10815. const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
  10816. {
  10817. if (!event)
  10818. return ERR_PTR(-EINVAL);
  10819. return &event->attr;
  10820. }
  10821. /*
  10822. * Inherit an event from parent task to child task.
  10823. *
  10824. * Returns:
  10825. * - valid pointer on success
  10826. * - NULL for orphaned events
  10827. * - IS_ERR() on error
  10828. */
  10829. static struct perf_event *
  10830. inherit_event(struct perf_event *parent_event,
  10831. struct task_struct *parent,
  10832. struct perf_event_context *parent_ctx,
  10833. struct task_struct *child,
  10834. struct perf_event *group_leader,
  10835. struct perf_event_context *child_ctx)
  10836. {
  10837. enum perf_event_state parent_state = parent_event->state;
  10838. struct perf_event *child_event;
  10839. unsigned long flags;
  10840. /*
  10841. * Instead of creating recursive hierarchies of events,
  10842. * we link inherited events back to the original parent,
  10843. * which has a filp for sure, which we use as the reference
  10844. * count:
  10845. */
  10846. if (parent_event->parent)
  10847. parent_event = parent_event->parent;
  10848. child_event = perf_event_alloc(&parent_event->attr,
  10849. parent_event->cpu,
  10850. child,
  10851. group_leader, parent_event,
  10852. NULL, NULL, -1);
  10853. if (IS_ERR(child_event))
  10854. return child_event;
  10855. if ((child_event->attach_state & PERF_ATTACH_TASK_DATA) &&
  10856. !child_ctx->task_ctx_data) {
  10857. struct pmu *pmu = child_event->pmu;
  10858. child_ctx->task_ctx_data = alloc_task_ctx_data(pmu);
  10859. if (!child_ctx->task_ctx_data) {
  10860. free_event(child_event);
  10861. return ERR_PTR(-ENOMEM);
  10862. }
  10863. }
  10864. /*
  10865. * is_orphaned_event() and list_add_tail(&parent_event->child_list)
  10866. * must be under the same lock in order to serialize against
  10867. * perf_event_release_kernel(), such that either we must observe
  10868. * is_orphaned_event() or they will observe us on the child_list.
  10869. */
  10870. mutex_lock(&parent_event->child_mutex);
  10871. if (is_orphaned_event(parent_event) ||
  10872. !atomic_long_inc_not_zero(&parent_event->refcount)) {
  10873. mutex_unlock(&parent_event->child_mutex);
  10874. /* task_ctx_data is freed with child_ctx */
  10875. free_event(child_event);
  10876. return NULL;
  10877. }
  10878. get_ctx(child_ctx);
  10879. /*
  10880. * Make the child state follow the state of the parent event,
  10881. * not its attr.disabled bit. We hold the parent's mutex,
  10882. * so we won't race with perf_event_{en, dis}able_family.
  10883. */
  10884. if (parent_state >= PERF_EVENT_STATE_INACTIVE)
  10885. child_event->state = PERF_EVENT_STATE_INACTIVE;
  10886. else
  10887. child_event->state = PERF_EVENT_STATE_OFF;
  10888. if (parent_event->attr.freq) {
  10889. u64 sample_period = parent_event->hw.sample_period;
  10890. struct hw_perf_event *hwc = &child_event->hw;
  10891. hwc->sample_period = sample_period;
  10892. hwc->last_period = sample_period;
  10893. local64_set(&hwc->period_left, sample_period);
  10894. }
  10895. child_event->ctx = child_ctx;
  10896. child_event->overflow_handler = parent_event->overflow_handler;
  10897. child_event->overflow_handler_context
  10898. = parent_event->overflow_handler_context;
  10899. /*
  10900. * Precalculate sample_data sizes
  10901. */
  10902. perf_event__header_size(child_event);
  10903. perf_event__id_header_size(child_event);
  10904. /*
  10905. * Link it up in the child's context:
  10906. */
  10907. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  10908. add_event_to_ctx(child_event, child_ctx);
  10909. child_event->attach_state |= PERF_ATTACH_CHILD;
  10910. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  10911. /*
  10912. * Link this into the parent event's child list
  10913. */
  10914. list_add_tail(&child_event->child_list, &parent_event->child_list);
  10915. mutex_unlock(&parent_event->child_mutex);
  10916. return child_event;
  10917. }
  10918. /*
  10919. * Inherits an event group.
  10920. *
  10921. * This will quietly suppress orphaned events; !inherit_event() is not an error.
  10922. * This matches with perf_event_release_kernel() removing all child events.
  10923. *
  10924. * Returns:
  10925. * - 0 on success
  10926. * - <0 on error
  10927. */
  10928. static int inherit_group(struct perf_event *parent_event,
  10929. struct task_struct *parent,
  10930. struct perf_event_context *parent_ctx,
  10931. struct task_struct *child,
  10932. struct perf_event_context *child_ctx)
  10933. {
  10934. struct perf_event *leader;
  10935. struct perf_event *sub;
  10936. struct perf_event *child_ctr;
  10937. leader = inherit_event(parent_event, parent, parent_ctx,
  10938. child, NULL, child_ctx);
  10939. if (IS_ERR(leader))
  10940. return PTR_ERR(leader);
  10941. /*
  10942. * @leader can be NULL here because of is_orphaned_event(). In this
  10943. * case inherit_event() will create individual events, similar to what
  10944. * perf_group_detach() would do anyway.
  10945. */
  10946. for_each_sibling_event(sub, parent_event) {
  10947. child_ctr = inherit_event(sub, parent, parent_ctx,
  10948. child, leader, child_ctx);
  10949. if (IS_ERR(child_ctr))
  10950. return PTR_ERR(child_ctr);
  10951. if (sub->aux_event == parent_event && child_ctr &&
  10952. !perf_get_aux_event(child_ctr, leader))
  10953. return -EINVAL;
  10954. }
  10955. leader->group_generation = parent_event->group_generation;
  10956. return 0;
  10957. }
  10958. /*
  10959. * Creates the child task context and tries to inherit the event-group.
  10960. *
  10961. * Clears @inherited_all on !attr.inherited or error. Note that we'll leave
  10962. * inherited_all set when we 'fail' to inherit an orphaned event; this is
  10963. * consistent with perf_event_release_kernel() removing all child events.
  10964. *
  10965. * Returns:
  10966. * - 0 on success
  10967. * - <0 on error
  10968. */
  10969. static int
  10970. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  10971. struct perf_event_context *parent_ctx,
  10972. struct task_struct *child, int ctxn,
  10973. u64 clone_flags, int *inherited_all)
  10974. {
  10975. int ret;
  10976. struct perf_event_context *child_ctx;
  10977. if (!event->attr.inherit ||
  10978. (event->attr.inherit_thread && !(clone_flags & CLONE_THREAD)) ||
  10979. /* Do not inherit if sigtrap and signal handlers were cleared. */
  10980. (event->attr.sigtrap && (clone_flags & CLONE_CLEAR_SIGHAND))) {
  10981. *inherited_all = 0;
  10982. return 0;
  10983. }
  10984. child_ctx = child->perf_event_ctxp[ctxn];
  10985. if (!child_ctx) {
  10986. /*
  10987. * This is executed from the parent task context, so
  10988. * inherit events that have been marked for cloning.
  10989. * First allocate and initialize a context for the
  10990. * child.
  10991. */
  10992. child_ctx = alloc_perf_context(parent_ctx->pmu, child);
  10993. if (!child_ctx)
  10994. return -ENOMEM;
  10995. child->perf_event_ctxp[ctxn] = child_ctx;
  10996. }
  10997. ret = inherit_group(event, parent, parent_ctx,
  10998. child, child_ctx);
  10999. if (ret)
  11000. *inherited_all = 0;
  11001. return ret;
  11002. }
  11003. /*
  11004. * Initialize the perf_event context in task_struct
  11005. */
  11006. static int perf_event_init_context(struct task_struct *child, int ctxn,
  11007. u64 clone_flags)
  11008. {
  11009. struct perf_event_context *child_ctx, *parent_ctx;
  11010. struct perf_event_context *cloned_ctx;
  11011. struct perf_event *event;
  11012. struct task_struct *parent = current;
  11013. int inherited_all = 1;
  11014. unsigned long flags;
  11015. int ret = 0;
  11016. if (likely(!parent->perf_event_ctxp[ctxn]))
  11017. return 0;
  11018. /*
  11019. * If the parent's context is a clone, pin it so it won't get
  11020. * swapped under us.
  11021. */
  11022. parent_ctx = perf_pin_task_context(parent, ctxn);
  11023. if (!parent_ctx)
  11024. return 0;
  11025. /*
  11026. * No need to check if parent_ctx != NULL here; since we saw
  11027. * it non-NULL earlier, the only reason for it to become NULL
  11028. * is if we exit, and since we're currently in the middle of
  11029. * a fork we can't be exiting at the same time.
  11030. */
  11031. /*
  11032. * Lock the parent list. No need to lock the child - not PID
  11033. * hashed yet and not running, so nobody can access it.
  11034. */
  11035. mutex_lock(&parent_ctx->mutex);
  11036. /*
  11037. * We dont have to disable NMIs - we are only looking at
  11038. * the list, not manipulating it:
  11039. */
  11040. perf_event_groups_for_each(event, &parent_ctx->pinned_groups) {
  11041. ret = inherit_task_group(event, parent, parent_ctx,
  11042. child, ctxn, clone_flags,
  11043. &inherited_all);
  11044. if (ret)
  11045. goto out_unlock;
  11046. }
  11047. /*
  11048. * We can't hold ctx->lock when iterating the ->flexible_group list due
  11049. * to allocations, but we need to prevent rotation because
  11050. * rotate_ctx() will change the list from interrupt context.
  11051. */
  11052. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  11053. parent_ctx->rotate_disable = 1;
  11054. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  11055. perf_event_groups_for_each(event, &parent_ctx->flexible_groups) {
  11056. ret = inherit_task_group(event, parent, parent_ctx,
  11057. child, ctxn, clone_flags,
  11058. &inherited_all);
  11059. if (ret)
  11060. goto out_unlock;
  11061. }
  11062. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  11063. parent_ctx->rotate_disable = 0;
  11064. child_ctx = child->perf_event_ctxp[ctxn];
  11065. if (child_ctx && inherited_all) {
  11066. /*
  11067. * Mark the child context as a clone of the parent
  11068. * context, or of whatever the parent is a clone of.
  11069. *
  11070. * Note that if the parent is a clone, the holding of
  11071. * parent_ctx->lock avoids it from being uncloned.
  11072. */
  11073. cloned_ctx = parent_ctx->parent_ctx;
  11074. if (cloned_ctx) {
  11075. child_ctx->parent_ctx = cloned_ctx;
  11076. child_ctx->parent_gen = parent_ctx->parent_gen;
  11077. } else {
  11078. child_ctx->parent_ctx = parent_ctx;
  11079. child_ctx->parent_gen = parent_ctx->generation;
  11080. }
  11081. get_ctx(child_ctx->parent_ctx);
  11082. }
  11083. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  11084. out_unlock:
  11085. mutex_unlock(&parent_ctx->mutex);
  11086. perf_unpin_context(parent_ctx);
  11087. put_ctx(parent_ctx);
  11088. return ret;
  11089. }
  11090. /*
  11091. * Initialize the perf_event context in task_struct
  11092. */
  11093. int perf_event_init_task(struct task_struct *child, u64 clone_flags)
  11094. {
  11095. int ctxn, ret;
  11096. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  11097. mutex_init(&child->perf_event_mutex);
  11098. INIT_LIST_HEAD(&child->perf_event_list);
  11099. for_each_task_context_nr(ctxn) {
  11100. ret = perf_event_init_context(child, ctxn, clone_flags);
  11101. if (ret) {
  11102. perf_event_free_task(child);
  11103. return ret;
  11104. }
  11105. }
  11106. return 0;
  11107. }
  11108. static void __init perf_event_init_all_cpus(void)
  11109. {
  11110. struct swevent_htable *swhash;
  11111. int cpu;
  11112. zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL);
  11113. for_each_possible_cpu(cpu) {
  11114. swhash = &per_cpu(swevent_htable, cpu);
  11115. mutex_init(&swhash->hlist_mutex);
  11116. INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
  11117. INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
  11118. raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
  11119. #ifdef CONFIG_CGROUP_PERF
  11120. INIT_LIST_HEAD(&per_cpu(cgrp_cpuctx_list, cpu));
  11121. #endif
  11122. INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
  11123. }
  11124. }
  11125. static void perf_swevent_init_cpu(unsigned int cpu)
  11126. {
  11127. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  11128. mutex_lock(&swhash->hlist_mutex);
  11129. if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
  11130. struct swevent_hlist *hlist;
  11131. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  11132. WARN_ON(!hlist);
  11133. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  11134. }
  11135. mutex_unlock(&swhash->hlist_mutex);
  11136. }
  11137. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
  11138. static void __perf_event_exit_context(void *__info)
  11139. {
  11140. struct perf_event_context *ctx = __info;
  11141. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  11142. struct perf_event *event;
  11143. raw_spin_lock(&ctx->lock);
  11144. ctx_sched_out(ctx, cpuctx, EVENT_TIME);
  11145. list_for_each_entry(event, &ctx->event_list, event_entry)
  11146. __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
  11147. raw_spin_unlock(&ctx->lock);
  11148. }
  11149. static void perf_event_exit_cpu_context(int cpu)
  11150. {
  11151. struct perf_cpu_context *cpuctx;
  11152. struct perf_event_context *ctx;
  11153. struct pmu *pmu;
  11154. mutex_lock(&pmus_lock);
  11155. list_for_each_entry(pmu, &pmus, entry) {
  11156. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  11157. ctx = &cpuctx->ctx;
  11158. mutex_lock(&ctx->mutex);
  11159. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  11160. cpuctx->online = 0;
  11161. mutex_unlock(&ctx->mutex);
  11162. }
  11163. cpumask_clear_cpu(cpu, perf_online_mask);
  11164. mutex_unlock(&pmus_lock);
  11165. }
  11166. #else
  11167. static void perf_event_exit_cpu_context(int cpu) { }
  11168. #endif
  11169. int perf_event_init_cpu(unsigned int cpu)
  11170. {
  11171. struct perf_cpu_context *cpuctx;
  11172. struct perf_event_context *ctx;
  11173. struct pmu *pmu;
  11174. perf_swevent_init_cpu(cpu);
  11175. mutex_lock(&pmus_lock);
  11176. cpumask_set_cpu(cpu, perf_online_mask);
  11177. list_for_each_entry(pmu, &pmus, entry) {
  11178. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  11179. ctx = &cpuctx->ctx;
  11180. mutex_lock(&ctx->mutex);
  11181. cpuctx->online = 1;
  11182. mutex_unlock(&ctx->mutex);
  11183. }
  11184. mutex_unlock(&pmus_lock);
  11185. return 0;
  11186. }
  11187. int perf_event_exit_cpu(unsigned int cpu)
  11188. {
  11189. perf_event_exit_cpu_context(cpu);
  11190. return 0;
  11191. }
  11192. static int
  11193. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  11194. {
  11195. int cpu;
  11196. for_each_online_cpu(cpu)
  11197. perf_event_exit_cpu(cpu);
  11198. return NOTIFY_OK;
  11199. }
  11200. /*
  11201. * Run the perf reboot notifier at the very last possible moment so that
  11202. * the generic watchdog code runs as long as possible.
  11203. */
  11204. static struct notifier_block perf_reboot_notifier = {
  11205. .notifier_call = perf_reboot,
  11206. .priority = INT_MIN,
  11207. };
  11208. void __init perf_event_init(void)
  11209. {
  11210. int ret;
  11211. idr_init(&pmu_idr);
  11212. perf_event_init_all_cpus();
  11213. init_srcu_struct(&pmus_srcu);
  11214. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  11215. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  11216. perf_pmu_register(&perf_task_clock, NULL, -1);
  11217. perf_tp_register();
  11218. perf_event_init_cpu(smp_processor_id());
  11219. register_reboot_notifier(&perf_reboot_notifier);
  11220. ret = init_hw_breakpoint();
  11221. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  11222. perf_event_cache = KMEM_CACHE(perf_event, SLAB_PANIC);
  11223. /*
  11224. * Build time assertion that we keep the data_head at the intended
  11225. * location. IOW, validation we got the __reserved[] size right.
  11226. */
  11227. BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
  11228. != 1024);
  11229. }
  11230. ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
  11231. char *page)
  11232. {
  11233. struct perf_pmu_events_attr *pmu_attr =
  11234. container_of(attr, struct perf_pmu_events_attr, attr);
  11235. if (pmu_attr->event_str)
  11236. return sprintf(page, "%s\n", pmu_attr->event_str);
  11237. return 0;
  11238. }
  11239. EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
  11240. static int __init perf_event_sysfs_init(void)
  11241. {
  11242. struct pmu *pmu;
  11243. int ret;
  11244. mutex_lock(&pmus_lock);
  11245. ret = bus_register(&pmu_bus);
  11246. if (ret)
  11247. goto unlock;
  11248. list_for_each_entry(pmu, &pmus, entry) {
  11249. if (!pmu->name || pmu->type < 0)
  11250. continue;
  11251. ret = pmu_dev_alloc(pmu);
  11252. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  11253. }
  11254. pmu_bus_running = 1;
  11255. ret = 0;
  11256. unlock:
  11257. mutex_unlock(&pmus_lock);
  11258. return ret;
  11259. }
  11260. device_initcall(perf_event_sysfs_init);
  11261. #ifdef CONFIG_CGROUP_PERF
  11262. static struct cgroup_subsys_state *
  11263. perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
  11264. {
  11265. struct perf_cgroup *jc;
  11266. jc = kzalloc(sizeof(*jc), GFP_KERNEL);
  11267. if (!jc)
  11268. return ERR_PTR(-ENOMEM);
  11269. jc->info = alloc_percpu(struct perf_cgroup_info);
  11270. if (!jc->info) {
  11271. kfree(jc);
  11272. return ERR_PTR(-ENOMEM);
  11273. }
  11274. return &jc->css;
  11275. }
  11276. static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
  11277. {
  11278. struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
  11279. free_percpu(jc->info);
  11280. kfree(jc);
  11281. }
  11282. static int perf_cgroup_css_online(struct cgroup_subsys_state *css)
  11283. {
  11284. perf_event_cgroup(css->cgroup);
  11285. return 0;
  11286. }
  11287. static int __perf_cgroup_move(void *info)
  11288. {
  11289. struct task_struct *task = info;
  11290. rcu_read_lock();
  11291. perf_cgroup_switch(task);
  11292. rcu_read_unlock();
  11293. return 0;
  11294. }
  11295. static void perf_cgroup_attach(struct cgroup_taskset *tset)
  11296. {
  11297. struct task_struct *task;
  11298. struct cgroup_subsys_state *css;
  11299. cgroup_taskset_for_each(task, css, tset)
  11300. task_function_call(task, __perf_cgroup_move, task);
  11301. }
  11302. struct cgroup_subsys perf_event_cgrp_subsys = {
  11303. .css_alloc = perf_cgroup_css_alloc,
  11304. .css_free = perf_cgroup_css_free,
  11305. .css_online = perf_cgroup_css_online,
  11306. .attach = perf_cgroup_attach,
  11307. /*
  11308. * Implicitly enable on dfl hierarchy so that perf events can
  11309. * always be filtered by cgroup2 path as long as perf_event
  11310. * controller is not mounted on a legacy hierarchy.
  11311. */
  11312. .implicit_on_dfl = true,
  11313. .threaded = true,
  11314. };
  11315. #endif /* CONFIG_CGROUP_PERF */
  11316. DEFINE_STATIC_CALL_RET0(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t);