core.c 199 KB

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