core.c 180 KB

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