core.c 210 KB

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