core.c 195 KB

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