core.c 201 KB

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