core.c 253 KB

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