core.c 194 KB

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