core.c 283 KB

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