perf_event.c 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2009 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/file.h>
  16. #include <linux/poll.h>
  17. #include <linux/slab.h>
  18. #include <linux/hash.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/dcache.h>
  21. #include <linux/percpu.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/vmstat.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/hardirq.h>
  26. #include <linux/rculist.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/anon_inodes.h>
  30. #include <linux/kernel_stat.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/ftrace_event.h>
  33. #include <linux/hw_breakpoint.h>
  34. #include <asm/irq_regs.h>
  35. /*
  36. * Each CPU has a list of per CPU events:
  37. */
  38. static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  39. int perf_max_events __read_mostly = 1;
  40. static int perf_reserved_percpu __read_mostly;
  41. static int perf_overcommit __read_mostly = 1;
  42. static atomic_t nr_events __read_mostly;
  43. static atomic_t nr_mmap_events __read_mostly;
  44. static atomic_t nr_comm_events __read_mostly;
  45. static atomic_t nr_task_events __read_mostly;
  46. /*
  47. * perf event paranoia level:
  48. * -1 - not paranoid at all
  49. * 0 - disallow raw tracepoint access for unpriv
  50. * 1 - disallow cpu events for unpriv
  51. * 2 - disallow kernel profiling for unpriv
  52. */
  53. int sysctl_perf_event_paranoid __read_mostly = 1;
  54. int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
  55. /*
  56. * max perf event sample rate
  57. */
  58. int sysctl_perf_event_sample_rate __read_mostly = 100000;
  59. static atomic64_t perf_event_id;
  60. /*
  61. * Lock for (sysadmin-configurable) event reservations:
  62. */
  63. static DEFINE_SPINLOCK(perf_resource_lock);
  64. /*
  65. * Architecture provided APIs - weak aliases:
  66. */
  67. extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
  68. {
  69. return NULL;
  70. }
  71. void __weak hw_perf_disable(void) { barrier(); }
  72. void __weak hw_perf_enable(void) { barrier(); }
  73. void __weak perf_event_print_debug(void) { }
  74. static DEFINE_PER_CPU(int, perf_disable_count);
  75. void perf_disable(void)
  76. {
  77. if (!__get_cpu_var(perf_disable_count)++)
  78. hw_perf_disable();
  79. }
  80. void perf_enable(void)
  81. {
  82. if (!--__get_cpu_var(perf_disable_count))
  83. hw_perf_enable();
  84. }
  85. static void get_ctx(struct perf_event_context *ctx)
  86. {
  87. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  88. }
  89. static void free_ctx(struct rcu_head *head)
  90. {
  91. struct perf_event_context *ctx;
  92. ctx = container_of(head, struct perf_event_context, rcu_head);
  93. kfree(ctx);
  94. }
  95. static void put_ctx(struct perf_event_context *ctx)
  96. {
  97. if (atomic_dec_and_test(&ctx->refcount)) {
  98. if (ctx->parent_ctx)
  99. put_ctx(ctx->parent_ctx);
  100. if (ctx->task)
  101. put_task_struct(ctx->task);
  102. call_rcu(&ctx->rcu_head, free_ctx);
  103. }
  104. }
  105. static void unclone_ctx(struct perf_event_context *ctx)
  106. {
  107. if (ctx->parent_ctx) {
  108. put_ctx(ctx->parent_ctx);
  109. ctx->parent_ctx = NULL;
  110. }
  111. }
  112. /*
  113. * If we inherit events we want to return the parent event id
  114. * to userspace.
  115. */
  116. static u64 primary_event_id(struct perf_event *event)
  117. {
  118. u64 id = event->id;
  119. if (event->parent)
  120. id = event->parent->id;
  121. return id;
  122. }
  123. /*
  124. * Get the perf_event_context for a task and lock it.
  125. * This has to cope with with the fact that until it is locked,
  126. * the context could get moved to another task.
  127. */
  128. static struct perf_event_context *
  129. perf_lock_task_context(struct task_struct *task, unsigned long *flags)
  130. {
  131. struct perf_event_context *ctx;
  132. rcu_read_lock();
  133. retry:
  134. ctx = rcu_dereference(task->perf_event_ctxp);
  135. if (ctx) {
  136. /*
  137. * If this context is a clone of another, it might
  138. * get swapped for another underneath us by
  139. * perf_event_task_sched_out, though the
  140. * rcu_read_lock() protects us from any context
  141. * getting freed. Lock the context and check if it
  142. * got swapped before we could get the lock, and retry
  143. * if so. If we locked the right context, then it
  144. * can't get swapped on us any more.
  145. */
  146. raw_spin_lock_irqsave(&ctx->lock, *flags);
  147. if (ctx != rcu_dereference(task->perf_event_ctxp)) {
  148. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  149. goto retry;
  150. }
  151. if (!atomic_inc_not_zero(&ctx->refcount)) {
  152. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  153. ctx = NULL;
  154. }
  155. }
  156. rcu_read_unlock();
  157. return ctx;
  158. }
  159. /*
  160. * Get the context for a task and increment its pin_count so it
  161. * can't get swapped to another task. This also increments its
  162. * reference count so that the context can't get freed.
  163. */
  164. static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
  165. {
  166. struct perf_event_context *ctx;
  167. unsigned long flags;
  168. ctx = perf_lock_task_context(task, &flags);
  169. if (ctx) {
  170. ++ctx->pin_count;
  171. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  172. }
  173. return ctx;
  174. }
  175. static void perf_unpin_context(struct perf_event_context *ctx)
  176. {
  177. unsigned long flags;
  178. raw_spin_lock_irqsave(&ctx->lock, flags);
  179. --ctx->pin_count;
  180. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  181. put_ctx(ctx);
  182. }
  183. static inline u64 perf_clock(void)
  184. {
  185. return cpu_clock(raw_smp_processor_id());
  186. }
  187. /*
  188. * Update the record of the current time in a context.
  189. */
  190. static void update_context_time(struct perf_event_context *ctx)
  191. {
  192. u64 now = perf_clock();
  193. ctx->time += now - ctx->timestamp;
  194. ctx->timestamp = now;
  195. }
  196. /*
  197. * Update the total_time_enabled and total_time_running fields for a event.
  198. */
  199. static void update_event_times(struct perf_event *event)
  200. {
  201. struct perf_event_context *ctx = event->ctx;
  202. u64 run_end;
  203. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  204. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  205. return;
  206. if (ctx->is_active)
  207. run_end = ctx->time;
  208. else
  209. run_end = event->tstamp_stopped;
  210. event->total_time_enabled = run_end - event->tstamp_enabled;
  211. if (event->state == PERF_EVENT_STATE_INACTIVE)
  212. run_end = event->tstamp_stopped;
  213. else
  214. run_end = ctx->time;
  215. event->total_time_running = run_end - event->tstamp_running;
  216. }
  217. /*
  218. * Update total_time_enabled and total_time_running for all events in a group.
  219. */
  220. static void update_group_times(struct perf_event *leader)
  221. {
  222. struct perf_event *event;
  223. update_event_times(leader);
  224. list_for_each_entry(event, &leader->sibling_list, group_entry)
  225. update_event_times(event);
  226. }
  227. static struct list_head *
  228. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  229. {
  230. if (event->attr.pinned)
  231. return &ctx->pinned_groups;
  232. else
  233. return &ctx->flexible_groups;
  234. }
  235. /*
  236. * Add a event from the lists for its context.
  237. * Must be called with ctx->mutex and ctx->lock held.
  238. */
  239. static void
  240. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  241. {
  242. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  243. event->attach_state |= PERF_ATTACH_CONTEXT;
  244. /*
  245. * If we're a stand alone event or group leader, we go to the context
  246. * list, group events are kept attached to the group so that
  247. * perf_group_detach can, at all times, locate all siblings.
  248. */
  249. if (event->group_leader == event) {
  250. struct list_head *list;
  251. if (is_software_event(event))
  252. event->group_flags |= PERF_GROUP_SOFTWARE;
  253. list = ctx_group_list(event, ctx);
  254. list_add_tail(&event->group_entry, list);
  255. }
  256. list_add_rcu(&event->event_entry, &ctx->event_list);
  257. ctx->nr_events++;
  258. if (event->attr.inherit_stat)
  259. ctx->nr_stat++;
  260. }
  261. static void perf_group_attach(struct perf_event *event)
  262. {
  263. struct perf_event *group_leader = event->group_leader;
  264. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
  265. event->attach_state |= PERF_ATTACH_GROUP;
  266. if (group_leader == event)
  267. return;
  268. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  269. !is_software_event(event))
  270. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  271. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  272. group_leader->nr_siblings++;
  273. }
  274. /*
  275. * Remove a event from the lists for its context.
  276. * Must be called with ctx->mutex and ctx->lock held.
  277. */
  278. static void
  279. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  280. {
  281. /*
  282. * We can have double detach due to exit/hot-unplug + close.
  283. */
  284. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  285. return;
  286. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  287. ctx->nr_events--;
  288. if (event->attr.inherit_stat)
  289. ctx->nr_stat--;
  290. list_del_rcu(&event->event_entry);
  291. if (event->group_leader == event)
  292. list_del_init(&event->group_entry);
  293. update_group_times(event);
  294. /*
  295. * If event was in error state, then keep it
  296. * that way, otherwise bogus counts will be
  297. * returned on read(). The only way to get out
  298. * of error state is by explicit re-enabling
  299. * of the event
  300. */
  301. if (event->state > PERF_EVENT_STATE_OFF)
  302. event->state = PERF_EVENT_STATE_OFF;
  303. }
  304. static void perf_group_detach(struct perf_event *event)
  305. {
  306. struct perf_event *sibling, *tmp;
  307. struct list_head *list = NULL;
  308. /*
  309. * We can have double detach due to exit/hot-unplug + close.
  310. */
  311. if (!(event->attach_state & PERF_ATTACH_GROUP))
  312. return;
  313. event->attach_state &= ~PERF_ATTACH_GROUP;
  314. /*
  315. * If this is a sibling, remove it from its group.
  316. */
  317. if (event->group_leader != event) {
  318. list_del_init(&event->group_entry);
  319. event->group_leader->nr_siblings--;
  320. return;
  321. }
  322. if (!list_empty(&event->group_entry))
  323. list = &event->group_entry;
  324. /*
  325. * If this was a group event with sibling events then
  326. * upgrade the siblings to singleton events by adding them
  327. * to whatever list we are on.
  328. */
  329. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  330. if (list)
  331. list_move_tail(&sibling->group_entry, list);
  332. sibling->group_leader = sibling;
  333. /* Inherit group flags from the previous leader */
  334. sibling->group_flags = event->group_flags;
  335. }
  336. }
  337. static void
  338. event_sched_out(struct perf_event *event,
  339. struct perf_cpu_context *cpuctx,
  340. struct perf_event_context *ctx)
  341. {
  342. if (event->state != PERF_EVENT_STATE_ACTIVE)
  343. return;
  344. event->state = PERF_EVENT_STATE_INACTIVE;
  345. if (event->pending_disable) {
  346. event->pending_disable = 0;
  347. event->state = PERF_EVENT_STATE_OFF;
  348. }
  349. event->tstamp_stopped = ctx->time;
  350. event->pmu->disable(event);
  351. event->oncpu = -1;
  352. if (!is_software_event(event))
  353. cpuctx->active_oncpu--;
  354. ctx->nr_active--;
  355. if (event->attr.exclusive || !cpuctx->active_oncpu)
  356. cpuctx->exclusive = 0;
  357. }
  358. static void
  359. group_sched_out(struct perf_event *group_event,
  360. struct perf_cpu_context *cpuctx,
  361. struct perf_event_context *ctx)
  362. {
  363. struct perf_event *event;
  364. if (group_event->state != PERF_EVENT_STATE_ACTIVE)
  365. return;
  366. event_sched_out(group_event, cpuctx, ctx);
  367. /*
  368. * Schedule out siblings (if any):
  369. */
  370. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  371. event_sched_out(event, cpuctx, ctx);
  372. if (group_event->attr.exclusive)
  373. cpuctx->exclusive = 0;
  374. }
  375. /*
  376. * Cross CPU call to remove a performance event
  377. *
  378. * We disable the event on the hardware level first. After that we
  379. * remove it from the context list.
  380. */
  381. static void __perf_event_remove_from_context(void *info)
  382. {
  383. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  384. struct perf_event *event = info;
  385. struct perf_event_context *ctx = event->ctx;
  386. /*
  387. * If this is a task context, we need to check whether it is
  388. * the current task context of this cpu. If not it has been
  389. * scheduled out before the smp call arrived.
  390. */
  391. if (ctx->task && cpuctx->task_ctx != ctx)
  392. return;
  393. raw_spin_lock(&ctx->lock);
  394. /*
  395. * Protect the list operation against NMI by disabling the
  396. * events on a global level.
  397. */
  398. perf_disable();
  399. event_sched_out(event, cpuctx, ctx);
  400. list_del_event(event, ctx);
  401. if (!ctx->task) {
  402. /*
  403. * Allow more per task events with respect to the
  404. * reservation:
  405. */
  406. cpuctx->max_pertask =
  407. min(perf_max_events - ctx->nr_events,
  408. perf_max_events - perf_reserved_percpu);
  409. }
  410. perf_enable();
  411. raw_spin_unlock(&ctx->lock);
  412. }
  413. /*
  414. * Remove the event from a task's (or a CPU's) list of events.
  415. *
  416. * Must be called with ctx->mutex held.
  417. *
  418. * CPU events are removed with a smp call. For task events we only
  419. * call when the task is on a CPU.
  420. *
  421. * If event->ctx is a cloned context, callers must make sure that
  422. * every task struct that event->ctx->task could possibly point to
  423. * remains valid. This is OK when called from perf_release since
  424. * that only calls us on the top-level context, which can't be a clone.
  425. * When called from perf_event_exit_task, it's OK because the
  426. * context has been detached from its task.
  427. */
  428. static void perf_event_remove_from_context(struct perf_event *event)
  429. {
  430. struct perf_event_context *ctx = event->ctx;
  431. struct task_struct *task = ctx->task;
  432. if (!task) {
  433. /*
  434. * Per cpu events are removed via an smp call and
  435. * the removal is always successful.
  436. */
  437. smp_call_function_single(event->cpu,
  438. __perf_event_remove_from_context,
  439. event, 1);
  440. return;
  441. }
  442. retry:
  443. task_oncpu_function_call(task, __perf_event_remove_from_context,
  444. event);
  445. raw_spin_lock_irq(&ctx->lock);
  446. /*
  447. * If the context is active we need to retry the smp call.
  448. */
  449. if (ctx->nr_active && !list_empty(&event->group_entry)) {
  450. raw_spin_unlock_irq(&ctx->lock);
  451. goto retry;
  452. }
  453. /*
  454. * The lock prevents that this context is scheduled in so we
  455. * can remove the event safely, if the call above did not
  456. * succeed.
  457. */
  458. if (!list_empty(&event->group_entry))
  459. list_del_event(event, ctx);
  460. raw_spin_unlock_irq(&ctx->lock);
  461. }
  462. /*
  463. * Cross CPU call to disable a performance event
  464. */
  465. static void __perf_event_disable(void *info)
  466. {
  467. struct perf_event *event = info;
  468. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  469. struct perf_event_context *ctx = event->ctx;
  470. /*
  471. * If this is a per-task event, need to check whether this
  472. * event's task is the current task on this cpu.
  473. */
  474. if (ctx->task && cpuctx->task_ctx != ctx)
  475. return;
  476. raw_spin_lock(&ctx->lock);
  477. /*
  478. * If the event is on, turn it off.
  479. * If it is in error state, leave it in error state.
  480. */
  481. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  482. update_context_time(ctx);
  483. update_group_times(event);
  484. if (event == event->group_leader)
  485. group_sched_out(event, cpuctx, ctx);
  486. else
  487. event_sched_out(event, cpuctx, ctx);
  488. event->state = PERF_EVENT_STATE_OFF;
  489. }
  490. raw_spin_unlock(&ctx->lock);
  491. }
  492. /*
  493. * Disable a event.
  494. *
  495. * If event->ctx is a cloned context, callers must make sure that
  496. * every task struct that event->ctx->task could possibly point to
  497. * remains valid. This condition is satisifed when called through
  498. * perf_event_for_each_child or perf_event_for_each because they
  499. * hold the top-level event's child_mutex, so any descendant that
  500. * goes to exit will block in sync_child_event.
  501. * When called from perf_pending_event it's OK because event->ctx
  502. * is the current context on this CPU and preemption is disabled,
  503. * hence we can't get into perf_event_task_sched_out for this context.
  504. */
  505. void perf_event_disable(struct perf_event *event)
  506. {
  507. struct perf_event_context *ctx = event->ctx;
  508. struct task_struct *task = ctx->task;
  509. if (!task) {
  510. /*
  511. * Disable the event on the cpu that it's on
  512. */
  513. smp_call_function_single(event->cpu, __perf_event_disable,
  514. event, 1);
  515. return;
  516. }
  517. retry:
  518. task_oncpu_function_call(task, __perf_event_disable, event);
  519. raw_spin_lock_irq(&ctx->lock);
  520. /*
  521. * If the event is still active, we need to retry the cross-call.
  522. */
  523. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  524. raw_spin_unlock_irq(&ctx->lock);
  525. goto retry;
  526. }
  527. /*
  528. * Since we have the lock this context can't be scheduled
  529. * in, so we can change the state safely.
  530. */
  531. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  532. update_group_times(event);
  533. event->state = PERF_EVENT_STATE_OFF;
  534. }
  535. raw_spin_unlock_irq(&ctx->lock);
  536. }
  537. static int
  538. event_sched_in(struct perf_event *event,
  539. struct perf_cpu_context *cpuctx,
  540. struct perf_event_context *ctx)
  541. {
  542. if (event->state <= PERF_EVENT_STATE_OFF)
  543. return 0;
  544. event->state = PERF_EVENT_STATE_ACTIVE;
  545. event->oncpu = smp_processor_id();
  546. /*
  547. * The new state must be visible before we turn it on in the hardware:
  548. */
  549. smp_wmb();
  550. if (event->pmu->enable(event)) {
  551. event->state = PERF_EVENT_STATE_INACTIVE;
  552. event->oncpu = -1;
  553. return -EAGAIN;
  554. }
  555. event->tstamp_running += ctx->time - event->tstamp_stopped;
  556. if (!is_software_event(event))
  557. cpuctx->active_oncpu++;
  558. ctx->nr_active++;
  559. if (event->attr.exclusive)
  560. cpuctx->exclusive = 1;
  561. return 0;
  562. }
  563. static int
  564. group_sched_in(struct perf_event *group_event,
  565. struct perf_cpu_context *cpuctx,
  566. struct perf_event_context *ctx)
  567. {
  568. struct perf_event *event, *partial_group = NULL;
  569. const struct pmu *pmu = group_event->pmu;
  570. bool txn = false;
  571. int ret;
  572. if (group_event->state == PERF_EVENT_STATE_OFF)
  573. return 0;
  574. /* Check if group transaction availabe */
  575. if (pmu->start_txn)
  576. txn = true;
  577. if (txn)
  578. pmu->start_txn(pmu);
  579. if (event_sched_in(group_event, cpuctx, ctx)) {
  580. if (txn)
  581. pmu->cancel_txn(pmu);
  582. return -EAGAIN;
  583. }
  584. /*
  585. * Schedule in siblings as one group (if any):
  586. */
  587. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  588. if (event_sched_in(event, cpuctx, ctx)) {
  589. partial_group = event;
  590. goto group_error;
  591. }
  592. }
  593. if (!txn)
  594. return 0;
  595. ret = pmu->commit_txn(pmu);
  596. if (!ret) {
  597. pmu->cancel_txn(pmu);
  598. return 0;
  599. }
  600. group_error:
  601. /*
  602. * Groups can be scheduled in as one unit only, so undo any
  603. * partial group before returning:
  604. */
  605. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  606. if (event == partial_group)
  607. break;
  608. event_sched_out(event, cpuctx, ctx);
  609. }
  610. event_sched_out(group_event, cpuctx, ctx);
  611. if (txn)
  612. pmu->cancel_txn(pmu);
  613. return -EAGAIN;
  614. }
  615. /*
  616. * Work out whether we can put this event group on the CPU now.
  617. */
  618. static int group_can_go_on(struct perf_event *event,
  619. struct perf_cpu_context *cpuctx,
  620. int can_add_hw)
  621. {
  622. /*
  623. * Groups consisting entirely of software events can always go on.
  624. */
  625. if (event->group_flags & PERF_GROUP_SOFTWARE)
  626. return 1;
  627. /*
  628. * If an exclusive group is already on, no other hardware
  629. * events can go on.
  630. */
  631. if (cpuctx->exclusive)
  632. return 0;
  633. /*
  634. * If this group is exclusive and there are already
  635. * events on the CPU, it can't go on.
  636. */
  637. if (event->attr.exclusive && cpuctx->active_oncpu)
  638. return 0;
  639. /*
  640. * Otherwise, try to add it if all previous groups were able
  641. * to go on.
  642. */
  643. return can_add_hw;
  644. }
  645. static void add_event_to_ctx(struct perf_event *event,
  646. struct perf_event_context *ctx)
  647. {
  648. list_add_event(event, ctx);
  649. perf_group_attach(event);
  650. event->tstamp_enabled = ctx->time;
  651. event->tstamp_running = ctx->time;
  652. event->tstamp_stopped = ctx->time;
  653. }
  654. /*
  655. * Cross CPU call to install and enable a performance event
  656. *
  657. * Must be called with ctx->mutex held
  658. */
  659. static void __perf_install_in_context(void *info)
  660. {
  661. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  662. struct perf_event *event = info;
  663. struct perf_event_context *ctx = event->ctx;
  664. struct perf_event *leader = event->group_leader;
  665. int err;
  666. /*
  667. * If this is a task context, we need to check whether it is
  668. * the current task context of this cpu. If not it has been
  669. * scheduled out before the smp call arrived.
  670. * Or possibly this is the right context but it isn't
  671. * on this cpu because it had no events.
  672. */
  673. if (ctx->task && cpuctx->task_ctx != ctx) {
  674. if (cpuctx->task_ctx || ctx->task != current)
  675. return;
  676. cpuctx->task_ctx = ctx;
  677. }
  678. raw_spin_lock(&ctx->lock);
  679. ctx->is_active = 1;
  680. update_context_time(ctx);
  681. /*
  682. * Protect the list operation against NMI by disabling the
  683. * events on a global level. NOP for non NMI based events.
  684. */
  685. perf_disable();
  686. add_event_to_ctx(event, ctx);
  687. if (event->cpu != -1 && event->cpu != smp_processor_id())
  688. goto unlock;
  689. /*
  690. * Don't put the event on if it is disabled or if
  691. * it is in a group and the group isn't on.
  692. */
  693. if (event->state != PERF_EVENT_STATE_INACTIVE ||
  694. (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
  695. goto unlock;
  696. /*
  697. * An exclusive event can't go on if there are already active
  698. * hardware events, and no hardware event can go on if there
  699. * is already an exclusive event on.
  700. */
  701. if (!group_can_go_on(event, cpuctx, 1))
  702. err = -EEXIST;
  703. else
  704. err = event_sched_in(event, cpuctx, ctx);
  705. if (err) {
  706. /*
  707. * This event couldn't go on. If it is in a group
  708. * then we have to pull the whole group off.
  709. * If the event group is pinned then put it in error state.
  710. */
  711. if (leader != event)
  712. group_sched_out(leader, cpuctx, ctx);
  713. if (leader->attr.pinned) {
  714. update_group_times(leader);
  715. leader->state = PERF_EVENT_STATE_ERROR;
  716. }
  717. }
  718. if (!err && !ctx->task && cpuctx->max_pertask)
  719. cpuctx->max_pertask--;
  720. unlock:
  721. perf_enable();
  722. raw_spin_unlock(&ctx->lock);
  723. }
  724. /*
  725. * Attach a performance event to a context
  726. *
  727. * First we add the event to the list with the hardware enable bit
  728. * in event->hw_config cleared.
  729. *
  730. * If the event is attached to a task which is on a CPU we use a smp
  731. * call to enable it in the task context. The task might have been
  732. * scheduled away, but we check this in the smp call again.
  733. *
  734. * Must be called with ctx->mutex held.
  735. */
  736. static void
  737. perf_install_in_context(struct perf_event_context *ctx,
  738. struct perf_event *event,
  739. int cpu)
  740. {
  741. struct task_struct *task = ctx->task;
  742. if (!task) {
  743. /*
  744. * Per cpu events are installed via an smp call and
  745. * the install is always successful.
  746. */
  747. smp_call_function_single(cpu, __perf_install_in_context,
  748. event, 1);
  749. return;
  750. }
  751. retry:
  752. task_oncpu_function_call(task, __perf_install_in_context,
  753. event);
  754. raw_spin_lock_irq(&ctx->lock);
  755. /*
  756. * we need to retry the smp call.
  757. */
  758. if (ctx->is_active && list_empty(&event->group_entry)) {
  759. raw_spin_unlock_irq(&ctx->lock);
  760. goto retry;
  761. }
  762. /*
  763. * The lock prevents that this context is scheduled in so we
  764. * can add the event safely, if it the call above did not
  765. * succeed.
  766. */
  767. if (list_empty(&event->group_entry))
  768. add_event_to_ctx(event, ctx);
  769. raw_spin_unlock_irq(&ctx->lock);
  770. }
  771. /*
  772. * Put a event into inactive state and update time fields.
  773. * Enabling the leader of a group effectively enables all
  774. * the group members that aren't explicitly disabled, so we
  775. * have to update their ->tstamp_enabled also.
  776. * Note: this works for group members as well as group leaders
  777. * since the non-leader members' sibling_lists will be empty.
  778. */
  779. static void __perf_event_mark_enabled(struct perf_event *event,
  780. struct perf_event_context *ctx)
  781. {
  782. struct perf_event *sub;
  783. event->state = PERF_EVENT_STATE_INACTIVE;
  784. event->tstamp_enabled = ctx->time - event->total_time_enabled;
  785. list_for_each_entry(sub, &event->sibling_list, group_entry)
  786. if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  787. sub->tstamp_enabled =
  788. ctx->time - sub->total_time_enabled;
  789. }
  790. /*
  791. * Cross CPU call to enable a performance event
  792. */
  793. static void __perf_event_enable(void *info)
  794. {
  795. struct perf_event *event = info;
  796. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  797. struct perf_event_context *ctx = event->ctx;
  798. struct perf_event *leader = event->group_leader;
  799. int err;
  800. /*
  801. * If this is a per-task event, need to check whether this
  802. * event's task is the current task on this cpu.
  803. */
  804. if (ctx->task && cpuctx->task_ctx != ctx) {
  805. if (cpuctx->task_ctx || ctx->task != current)
  806. return;
  807. cpuctx->task_ctx = ctx;
  808. }
  809. raw_spin_lock(&ctx->lock);
  810. ctx->is_active = 1;
  811. update_context_time(ctx);
  812. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  813. goto unlock;
  814. __perf_event_mark_enabled(event, ctx);
  815. if (event->cpu != -1 && event->cpu != smp_processor_id())
  816. goto unlock;
  817. /*
  818. * If the event is in a group and isn't the group leader,
  819. * then don't put it on unless the group is on.
  820. */
  821. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  822. goto unlock;
  823. if (!group_can_go_on(event, cpuctx, 1)) {
  824. err = -EEXIST;
  825. } else {
  826. perf_disable();
  827. if (event == leader)
  828. err = group_sched_in(event, cpuctx, ctx);
  829. else
  830. err = event_sched_in(event, cpuctx, ctx);
  831. perf_enable();
  832. }
  833. if (err) {
  834. /*
  835. * If this event can't go on and it's part of a
  836. * group, then the whole group has to come off.
  837. */
  838. if (leader != event)
  839. group_sched_out(leader, cpuctx, ctx);
  840. if (leader->attr.pinned) {
  841. update_group_times(leader);
  842. leader->state = PERF_EVENT_STATE_ERROR;
  843. }
  844. }
  845. unlock:
  846. raw_spin_unlock(&ctx->lock);
  847. }
  848. /*
  849. * Enable a event.
  850. *
  851. * If event->ctx is a cloned context, callers must make sure that
  852. * every task struct that event->ctx->task could possibly point to
  853. * remains valid. This condition is satisfied when called through
  854. * perf_event_for_each_child or perf_event_for_each as described
  855. * for perf_event_disable.
  856. */
  857. void perf_event_enable(struct perf_event *event)
  858. {
  859. struct perf_event_context *ctx = event->ctx;
  860. struct task_struct *task = ctx->task;
  861. if (!task) {
  862. /*
  863. * Enable the event on the cpu that it's on
  864. */
  865. smp_call_function_single(event->cpu, __perf_event_enable,
  866. event, 1);
  867. return;
  868. }
  869. raw_spin_lock_irq(&ctx->lock);
  870. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  871. goto out;
  872. /*
  873. * If the event is in error state, clear that first.
  874. * That way, if we see the event in error state below, we
  875. * know that it has gone back into error state, as distinct
  876. * from the task having been scheduled away before the
  877. * cross-call arrived.
  878. */
  879. if (event->state == PERF_EVENT_STATE_ERROR)
  880. event->state = PERF_EVENT_STATE_OFF;
  881. retry:
  882. raw_spin_unlock_irq(&ctx->lock);
  883. task_oncpu_function_call(task, __perf_event_enable, event);
  884. raw_spin_lock_irq(&ctx->lock);
  885. /*
  886. * If the context is active and the event is still off,
  887. * we need to retry the cross-call.
  888. */
  889. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
  890. goto retry;
  891. /*
  892. * Since we have the lock this context can't be scheduled
  893. * in, so we can change the state safely.
  894. */
  895. if (event->state == PERF_EVENT_STATE_OFF)
  896. __perf_event_mark_enabled(event, ctx);
  897. out:
  898. raw_spin_unlock_irq(&ctx->lock);
  899. }
  900. static int perf_event_refresh(struct perf_event *event, int refresh)
  901. {
  902. /*
  903. * not supported on inherited events
  904. */
  905. if (event->attr.inherit)
  906. return -EINVAL;
  907. atomic_add(refresh, &event->event_limit);
  908. perf_event_enable(event);
  909. return 0;
  910. }
  911. enum event_type_t {
  912. EVENT_FLEXIBLE = 0x1,
  913. EVENT_PINNED = 0x2,
  914. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  915. };
  916. static void ctx_sched_out(struct perf_event_context *ctx,
  917. struct perf_cpu_context *cpuctx,
  918. enum event_type_t event_type)
  919. {
  920. struct perf_event *event;
  921. raw_spin_lock(&ctx->lock);
  922. ctx->is_active = 0;
  923. if (likely(!ctx->nr_events))
  924. goto out;
  925. update_context_time(ctx);
  926. perf_disable();
  927. if (!ctx->nr_active)
  928. goto out_enable;
  929. if (event_type & EVENT_PINNED)
  930. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  931. group_sched_out(event, cpuctx, ctx);
  932. if (event_type & EVENT_FLEXIBLE)
  933. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  934. group_sched_out(event, cpuctx, ctx);
  935. out_enable:
  936. perf_enable();
  937. out:
  938. raw_spin_unlock(&ctx->lock);
  939. }
  940. /*
  941. * Test whether two contexts are equivalent, i.e. whether they
  942. * have both been cloned from the same version of the same context
  943. * and they both have the same number of enabled events.
  944. * If the number of enabled events is the same, then the set
  945. * of enabled events should be the same, because these are both
  946. * inherited contexts, therefore we can't access individual events
  947. * in them directly with an fd; we can only enable/disable all
  948. * events via prctl, or enable/disable all events in a family
  949. * via ioctl, which will have the same effect on both contexts.
  950. */
  951. static int context_equiv(struct perf_event_context *ctx1,
  952. struct perf_event_context *ctx2)
  953. {
  954. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  955. && ctx1->parent_gen == ctx2->parent_gen
  956. && !ctx1->pin_count && !ctx2->pin_count;
  957. }
  958. static void __perf_event_sync_stat(struct perf_event *event,
  959. struct perf_event *next_event)
  960. {
  961. u64 value;
  962. if (!event->attr.inherit_stat)
  963. return;
  964. /*
  965. * Update the event value, we cannot use perf_event_read()
  966. * because we're in the middle of a context switch and have IRQs
  967. * disabled, which upsets smp_call_function_single(), however
  968. * we know the event must be on the current CPU, therefore we
  969. * don't need to use it.
  970. */
  971. switch (event->state) {
  972. case PERF_EVENT_STATE_ACTIVE:
  973. event->pmu->read(event);
  974. /* fall-through */
  975. case PERF_EVENT_STATE_INACTIVE:
  976. update_event_times(event);
  977. break;
  978. default:
  979. break;
  980. }
  981. /*
  982. * In order to keep per-task stats reliable we need to flip the event
  983. * values when we flip the contexts.
  984. */
  985. value = atomic64_read(&next_event->count);
  986. value = atomic64_xchg(&event->count, value);
  987. atomic64_set(&next_event->count, value);
  988. swap(event->total_time_enabled, next_event->total_time_enabled);
  989. swap(event->total_time_running, next_event->total_time_running);
  990. /*
  991. * Since we swizzled the values, update the user visible data too.
  992. */
  993. perf_event_update_userpage(event);
  994. perf_event_update_userpage(next_event);
  995. }
  996. #define list_next_entry(pos, member) \
  997. list_entry(pos->member.next, typeof(*pos), member)
  998. static void perf_event_sync_stat(struct perf_event_context *ctx,
  999. struct perf_event_context *next_ctx)
  1000. {
  1001. struct perf_event *event, *next_event;
  1002. if (!ctx->nr_stat)
  1003. return;
  1004. update_context_time(ctx);
  1005. event = list_first_entry(&ctx->event_list,
  1006. struct perf_event, event_entry);
  1007. next_event = list_first_entry(&next_ctx->event_list,
  1008. struct perf_event, event_entry);
  1009. while (&event->event_entry != &ctx->event_list &&
  1010. &next_event->event_entry != &next_ctx->event_list) {
  1011. __perf_event_sync_stat(event, next_event);
  1012. event = list_next_entry(event, event_entry);
  1013. next_event = list_next_entry(next_event, event_entry);
  1014. }
  1015. }
  1016. /*
  1017. * Called from scheduler to remove the events of the current task,
  1018. * with interrupts disabled.
  1019. *
  1020. * We stop each event and update the event value in event->count.
  1021. *
  1022. * This does not protect us against NMI, but disable()
  1023. * sets the disabled bit in the control field of event _before_
  1024. * accessing the event control register. If a NMI hits, then it will
  1025. * not restart the event.
  1026. */
  1027. void perf_event_task_sched_out(struct task_struct *task,
  1028. struct task_struct *next)
  1029. {
  1030. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1031. struct perf_event_context *ctx = task->perf_event_ctxp;
  1032. struct perf_event_context *next_ctx;
  1033. struct perf_event_context *parent;
  1034. int do_switch = 1;
  1035. perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
  1036. if (likely(!ctx || !cpuctx->task_ctx))
  1037. return;
  1038. rcu_read_lock();
  1039. parent = rcu_dereference(ctx->parent_ctx);
  1040. next_ctx = next->perf_event_ctxp;
  1041. if (parent && next_ctx &&
  1042. rcu_dereference(next_ctx->parent_ctx) == parent) {
  1043. /*
  1044. * Looks like the two contexts are clones, so we might be
  1045. * able to optimize the context switch. We lock both
  1046. * contexts and check that they are clones under the
  1047. * lock (including re-checking that neither has been
  1048. * uncloned in the meantime). It doesn't matter which
  1049. * order we take the locks because no other cpu could
  1050. * be trying to lock both of these tasks.
  1051. */
  1052. raw_spin_lock(&ctx->lock);
  1053. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  1054. if (context_equiv(ctx, next_ctx)) {
  1055. /*
  1056. * XXX do we need a memory barrier of sorts
  1057. * wrt to rcu_dereference() of perf_event_ctxp
  1058. */
  1059. task->perf_event_ctxp = next_ctx;
  1060. next->perf_event_ctxp = ctx;
  1061. ctx->task = next;
  1062. next_ctx->task = task;
  1063. do_switch = 0;
  1064. perf_event_sync_stat(ctx, next_ctx);
  1065. }
  1066. raw_spin_unlock(&next_ctx->lock);
  1067. raw_spin_unlock(&ctx->lock);
  1068. }
  1069. rcu_read_unlock();
  1070. if (do_switch) {
  1071. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1072. cpuctx->task_ctx = NULL;
  1073. }
  1074. }
  1075. static void task_ctx_sched_out(struct perf_event_context *ctx,
  1076. enum event_type_t event_type)
  1077. {
  1078. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1079. if (!cpuctx->task_ctx)
  1080. return;
  1081. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  1082. return;
  1083. ctx_sched_out(ctx, cpuctx, event_type);
  1084. cpuctx->task_ctx = NULL;
  1085. }
  1086. /*
  1087. * Called with IRQs disabled
  1088. */
  1089. static void __perf_event_task_sched_out(struct perf_event_context *ctx)
  1090. {
  1091. task_ctx_sched_out(ctx, EVENT_ALL);
  1092. }
  1093. /*
  1094. * Called with IRQs disabled
  1095. */
  1096. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  1097. enum event_type_t event_type)
  1098. {
  1099. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  1100. }
  1101. static void
  1102. ctx_pinned_sched_in(struct perf_event_context *ctx,
  1103. struct perf_cpu_context *cpuctx)
  1104. {
  1105. struct perf_event *event;
  1106. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1107. if (event->state <= PERF_EVENT_STATE_OFF)
  1108. continue;
  1109. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1110. continue;
  1111. if (group_can_go_on(event, cpuctx, 1))
  1112. group_sched_in(event, cpuctx, ctx);
  1113. /*
  1114. * If this pinned group hasn't been scheduled,
  1115. * put it in error state.
  1116. */
  1117. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1118. update_group_times(event);
  1119. event->state = PERF_EVENT_STATE_ERROR;
  1120. }
  1121. }
  1122. }
  1123. static void
  1124. ctx_flexible_sched_in(struct perf_event_context *ctx,
  1125. struct perf_cpu_context *cpuctx)
  1126. {
  1127. struct perf_event *event;
  1128. int can_add_hw = 1;
  1129. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1130. /* Ignore events in OFF or ERROR state */
  1131. if (event->state <= PERF_EVENT_STATE_OFF)
  1132. continue;
  1133. /*
  1134. * Listen to the 'cpu' scheduling filter constraint
  1135. * of events:
  1136. */
  1137. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1138. continue;
  1139. if (group_can_go_on(event, cpuctx, can_add_hw))
  1140. if (group_sched_in(event, cpuctx, ctx))
  1141. can_add_hw = 0;
  1142. }
  1143. }
  1144. static void
  1145. ctx_sched_in(struct perf_event_context *ctx,
  1146. struct perf_cpu_context *cpuctx,
  1147. enum event_type_t event_type)
  1148. {
  1149. raw_spin_lock(&ctx->lock);
  1150. ctx->is_active = 1;
  1151. if (likely(!ctx->nr_events))
  1152. goto out;
  1153. ctx->timestamp = perf_clock();
  1154. perf_disable();
  1155. /*
  1156. * First go through the list and put on any pinned groups
  1157. * in order to give them the best chance of going on.
  1158. */
  1159. if (event_type & EVENT_PINNED)
  1160. ctx_pinned_sched_in(ctx, cpuctx);
  1161. /* Then walk through the lower prio flexible groups */
  1162. if (event_type & EVENT_FLEXIBLE)
  1163. ctx_flexible_sched_in(ctx, cpuctx);
  1164. perf_enable();
  1165. out:
  1166. raw_spin_unlock(&ctx->lock);
  1167. }
  1168. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  1169. enum event_type_t event_type)
  1170. {
  1171. struct perf_event_context *ctx = &cpuctx->ctx;
  1172. ctx_sched_in(ctx, cpuctx, event_type);
  1173. }
  1174. static void task_ctx_sched_in(struct task_struct *task,
  1175. enum event_type_t event_type)
  1176. {
  1177. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1178. struct perf_event_context *ctx = task->perf_event_ctxp;
  1179. if (likely(!ctx))
  1180. return;
  1181. if (cpuctx->task_ctx == ctx)
  1182. return;
  1183. ctx_sched_in(ctx, cpuctx, event_type);
  1184. cpuctx->task_ctx = ctx;
  1185. }
  1186. /*
  1187. * Called from scheduler to add the events of the current task
  1188. * with interrupts disabled.
  1189. *
  1190. * We restore the event value and then enable it.
  1191. *
  1192. * This does not protect us against NMI, but enable()
  1193. * sets the enabled bit in the control field of event _before_
  1194. * accessing the event control register. If a NMI hits, then it will
  1195. * keep the event running.
  1196. */
  1197. void perf_event_task_sched_in(struct task_struct *task)
  1198. {
  1199. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1200. struct perf_event_context *ctx = task->perf_event_ctxp;
  1201. if (likely(!ctx))
  1202. return;
  1203. if (cpuctx->task_ctx == ctx)
  1204. return;
  1205. perf_disable();
  1206. /*
  1207. * We want to keep the following priority order:
  1208. * cpu pinned (that don't need to move), task pinned,
  1209. * cpu flexible, task flexible.
  1210. */
  1211. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1212. ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
  1213. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1214. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
  1215. cpuctx->task_ctx = ctx;
  1216. perf_enable();
  1217. }
  1218. #define MAX_INTERRUPTS (~0ULL)
  1219. static void perf_log_throttle(struct perf_event *event, int enable);
  1220. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  1221. {
  1222. u64 frequency = event->attr.sample_freq;
  1223. u64 sec = NSEC_PER_SEC;
  1224. u64 divisor, dividend;
  1225. int count_fls, nsec_fls, frequency_fls, sec_fls;
  1226. count_fls = fls64(count);
  1227. nsec_fls = fls64(nsec);
  1228. frequency_fls = fls64(frequency);
  1229. sec_fls = 30;
  1230. /*
  1231. * We got @count in @nsec, with a target of sample_freq HZ
  1232. * the target period becomes:
  1233. *
  1234. * @count * 10^9
  1235. * period = -------------------
  1236. * @nsec * sample_freq
  1237. *
  1238. */
  1239. /*
  1240. * Reduce accuracy by one bit such that @a and @b converge
  1241. * to a similar magnitude.
  1242. */
  1243. #define REDUCE_FLS(a, b) \
  1244. do { \
  1245. if (a##_fls > b##_fls) { \
  1246. a >>= 1; \
  1247. a##_fls--; \
  1248. } else { \
  1249. b >>= 1; \
  1250. b##_fls--; \
  1251. } \
  1252. } while (0)
  1253. /*
  1254. * Reduce accuracy until either term fits in a u64, then proceed with
  1255. * the other, so that finally we can do a u64/u64 division.
  1256. */
  1257. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  1258. REDUCE_FLS(nsec, frequency);
  1259. REDUCE_FLS(sec, count);
  1260. }
  1261. if (count_fls + sec_fls > 64) {
  1262. divisor = nsec * frequency;
  1263. while (count_fls + sec_fls > 64) {
  1264. REDUCE_FLS(count, sec);
  1265. divisor >>= 1;
  1266. }
  1267. dividend = count * sec;
  1268. } else {
  1269. dividend = count * sec;
  1270. while (nsec_fls + frequency_fls > 64) {
  1271. REDUCE_FLS(nsec, frequency);
  1272. dividend >>= 1;
  1273. }
  1274. divisor = nsec * frequency;
  1275. }
  1276. if (!divisor)
  1277. return dividend;
  1278. return div64_u64(dividend, divisor);
  1279. }
  1280. static void perf_event_stop(struct perf_event *event)
  1281. {
  1282. if (!event->pmu->stop)
  1283. return event->pmu->disable(event);
  1284. return event->pmu->stop(event);
  1285. }
  1286. static int perf_event_start(struct perf_event *event)
  1287. {
  1288. if (!event->pmu->start)
  1289. return event->pmu->enable(event);
  1290. return event->pmu->start(event);
  1291. }
  1292. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
  1293. {
  1294. struct hw_perf_event *hwc = &event->hw;
  1295. s64 period, sample_period;
  1296. s64 delta;
  1297. period = perf_calculate_period(event, nsec, count);
  1298. delta = (s64)(period - hwc->sample_period);
  1299. delta = (delta + 7) / 8; /* low pass filter */
  1300. sample_period = hwc->sample_period + delta;
  1301. if (!sample_period)
  1302. sample_period = 1;
  1303. hwc->sample_period = sample_period;
  1304. if (atomic64_read(&hwc->period_left) > 8*sample_period) {
  1305. perf_disable();
  1306. perf_event_stop(event);
  1307. atomic64_set(&hwc->period_left, 0);
  1308. perf_event_start(event);
  1309. perf_enable();
  1310. }
  1311. }
  1312. static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
  1313. {
  1314. struct perf_event *event;
  1315. struct hw_perf_event *hwc;
  1316. u64 interrupts, now;
  1317. s64 delta;
  1318. raw_spin_lock(&ctx->lock);
  1319. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  1320. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1321. continue;
  1322. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1323. continue;
  1324. hwc = &event->hw;
  1325. interrupts = hwc->interrupts;
  1326. hwc->interrupts = 0;
  1327. /*
  1328. * unthrottle events on the tick
  1329. */
  1330. if (interrupts == MAX_INTERRUPTS) {
  1331. perf_log_throttle(event, 1);
  1332. perf_disable();
  1333. event->pmu->unthrottle(event);
  1334. perf_enable();
  1335. }
  1336. if (!event->attr.freq || !event->attr.sample_freq)
  1337. continue;
  1338. perf_disable();
  1339. event->pmu->read(event);
  1340. now = atomic64_read(&event->count);
  1341. delta = now - hwc->freq_count_stamp;
  1342. hwc->freq_count_stamp = now;
  1343. if (delta > 0)
  1344. perf_adjust_period(event, TICK_NSEC, delta);
  1345. perf_enable();
  1346. }
  1347. raw_spin_unlock(&ctx->lock);
  1348. }
  1349. /*
  1350. * Round-robin a context's events:
  1351. */
  1352. static void rotate_ctx(struct perf_event_context *ctx)
  1353. {
  1354. raw_spin_lock(&ctx->lock);
  1355. /* Rotate the first entry last of non-pinned groups */
  1356. list_rotate_left(&ctx->flexible_groups);
  1357. raw_spin_unlock(&ctx->lock);
  1358. }
  1359. void perf_event_task_tick(struct task_struct *curr)
  1360. {
  1361. struct perf_cpu_context *cpuctx;
  1362. struct perf_event_context *ctx;
  1363. int rotate = 0;
  1364. if (!atomic_read(&nr_events))
  1365. return;
  1366. cpuctx = &__get_cpu_var(perf_cpu_context);
  1367. if (cpuctx->ctx.nr_events &&
  1368. cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  1369. rotate = 1;
  1370. ctx = curr->perf_event_ctxp;
  1371. if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
  1372. rotate = 1;
  1373. perf_ctx_adjust_freq(&cpuctx->ctx);
  1374. if (ctx)
  1375. perf_ctx_adjust_freq(ctx);
  1376. if (!rotate)
  1377. return;
  1378. perf_disable();
  1379. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1380. if (ctx)
  1381. task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
  1382. rotate_ctx(&cpuctx->ctx);
  1383. if (ctx)
  1384. rotate_ctx(ctx);
  1385. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1386. if (ctx)
  1387. task_ctx_sched_in(curr, EVENT_FLEXIBLE);
  1388. perf_enable();
  1389. }
  1390. static int event_enable_on_exec(struct perf_event *event,
  1391. struct perf_event_context *ctx)
  1392. {
  1393. if (!event->attr.enable_on_exec)
  1394. return 0;
  1395. event->attr.enable_on_exec = 0;
  1396. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1397. return 0;
  1398. __perf_event_mark_enabled(event, ctx);
  1399. return 1;
  1400. }
  1401. /*
  1402. * Enable all of a task's events that have been marked enable-on-exec.
  1403. * This expects task == current.
  1404. */
  1405. static void perf_event_enable_on_exec(struct task_struct *task)
  1406. {
  1407. struct perf_event_context *ctx;
  1408. struct perf_event *event;
  1409. unsigned long flags;
  1410. int enabled = 0;
  1411. int ret;
  1412. local_irq_save(flags);
  1413. ctx = task->perf_event_ctxp;
  1414. if (!ctx || !ctx->nr_events)
  1415. goto out;
  1416. __perf_event_task_sched_out(ctx);
  1417. raw_spin_lock(&ctx->lock);
  1418. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1419. ret = event_enable_on_exec(event, ctx);
  1420. if (ret)
  1421. enabled = 1;
  1422. }
  1423. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1424. ret = event_enable_on_exec(event, ctx);
  1425. if (ret)
  1426. enabled = 1;
  1427. }
  1428. /*
  1429. * Unclone this context if we enabled any event.
  1430. */
  1431. if (enabled)
  1432. unclone_ctx(ctx);
  1433. raw_spin_unlock(&ctx->lock);
  1434. perf_event_task_sched_in(task);
  1435. out:
  1436. local_irq_restore(flags);
  1437. }
  1438. /*
  1439. * Cross CPU call to read the hardware event
  1440. */
  1441. static void __perf_event_read(void *info)
  1442. {
  1443. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  1444. struct perf_event *event = info;
  1445. struct perf_event_context *ctx = event->ctx;
  1446. /*
  1447. * If this is a task context, we need to check whether it is
  1448. * the current task context of this cpu. If not it has been
  1449. * scheduled out before the smp call arrived. In that case
  1450. * event->count would have been updated to a recent sample
  1451. * when the event was scheduled out.
  1452. */
  1453. if (ctx->task && cpuctx->task_ctx != ctx)
  1454. return;
  1455. raw_spin_lock(&ctx->lock);
  1456. update_context_time(ctx);
  1457. update_event_times(event);
  1458. raw_spin_unlock(&ctx->lock);
  1459. event->pmu->read(event);
  1460. }
  1461. static u64 perf_event_read(struct perf_event *event)
  1462. {
  1463. /*
  1464. * If event is enabled and currently active on a CPU, update the
  1465. * value in the event structure:
  1466. */
  1467. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1468. smp_call_function_single(event->oncpu,
  1469. __perf_event_read, event, 1);
  1470. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1471. struct perf_event_context *ctx = event->ctx;
  1472. unsigned long flags;
  1473. raw_spin_lock_irqsave(&ctx->lock, flags);
  1474. update_context_time(ctx);
  1475. update_event_times(event);
  1476. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1477. }
  1478. return atomic64_read(&event->count);
  1479. }
  1480. /*
  1481. * Initialize the perf_event context in a task_struct:
  1482. */
  1483. static void
  1484. __perf_event_init_context(struct perf_event_context *ctx,
  1485. struct task_struct *task)
  1486. {
  1487. raw_spin_lock_init(&ctx->lock);
  1488. mutex_init(&ctx->mutex);
  1489. INIT_LIST_HEAD(&ctx->pinned_groups);
  1490. INIT_LIST_HEAD(&ctx->flexible_groups);
  1491. INIT_LIST_HEAD(&ctx->event_list);
  1492. atomic_set(&ctx->refcount, 1);
  1493. ctx->task = task;
  1494. }
  1495. static struct perf_event_context *find_get_context(pid_t pid, int cpu)
  1496. {
  1497. struct perf_event_context *ctx;
  1498. struct perf_cpu_context *cpuctx;
  1499. struct task_struct *task;
  1500. unsigned long flags;
  1501. int err;
  1502. if (pid == -1 && cpu != -1) {
  1503. /* Must be root to operate on a CPU event: */
  1504. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1505. return ERR_PTR(-EACCES);
  1506. if (cpu < 0 || cpu >= nr_cpumask_bits)
  1507. return ERR_PTR(-EINVAL);
  1508. /*
  1509. * We could be clever and allow to attach a event to an
  1510. * offline CPU and activate it when the CPU comes up, but
  1511. * that's for later.
  1512. */
  1513. if (!cpu_online(cpu))
  1514. return ERR_PTR(-ENODEV);
  1515. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1516. ctx = &cpuctx->ctx;
  1517. get_ctx(ctx);
  1518. return ctx;
  1519. }
  1520. rcu_read_lock();
  1521. if (!pid)
  1522. task = current;
  1523. else
  1524. task = find_task_by_vpid(pid);
  1525. if (task)
  1526. get_task_struct(task);
  1527. rcu_read_unlock();
  1528. if (!task)
  1529. return ERR_PTR(-ESRCH);
  1530. /*
  1531. * Can't attach events to a dying task.
  1532. */
  1533. err = -ESRCH;
  1534. if (task->flags & PF_EXITING)
  1535. goto errout;
  1536. /* Reuse ptrace permission checks for now. */
  1537. err = -EACCES;
  1538. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1539. goto errout;
  1540. retry:
  1541. ctx = perf_lock_task_context(task, &flags);
  1542. if (ctx) {
  1543. unclone_ctx(ctx);
  1544. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1545. }
  1546. if (!ctx) {
  1547. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  1548. err = -ENOMEM;
  1549. if (!ctx)
  1550. goto errout;
  1551. __perf_event_init_context(ctx, task);
  1552. get_ctx(ctx);
  1553. if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
  1554. /*
  1555. * We raced with some other task; use
  1556. * the context they set.
  1557. */
  1558. kfree(ctx);
  1559. goto retry;
  1560. }
  1561. get_task_struct(task);
  1562. }
  1563. put_task_struct(task);
  1564. return ctx;
  1565. errout:
  1566. put_task_struct(task);
  1567. return ERR_PTR(err);
  1568. }
  1569. static void perf_event_free_filter(struct perf_event *event);
  1570. static void free_event_rcu(struct rcu_head *head)
  1571. {
  1572. struct perf_event *event;
  1573. event = container_of(head, struct perf_event, rcu_head);
  1574. if (event->ns)
  1575. put_pid_ns(event->ns);
  1576. perf_event_free_filter(event);
  1577. kfree(event);
  1578. }
  1579. static void perf_pending_sync(struct perf_event *event);
  1580. static void perf_mmap_data_put(struct perf_mmap_data *data);
  1581. static void free_event(struct perf_event *event)
  1582. {
  1583. perf_pending_sync(event);
  1584. if (!event->parent) {
  1585. atomic_dec(&nr_events);
  1586. if (event->attr.mmap)
  1587. atomic_dec(&nr_mmap_events);
  1588. if (event->attr.comm)
  1589. atomic_dec(&nr_comm_events);
  1590. if (event->attr.task)
  1591. atomic_dec(&nr_task_events);
  1592. }
  1593. if (event->data) {
  1594. perf_mmap_data_put(event->data);
  1595. event->data = NULL;
  1596. }
  1597. if (event->destroy)
  1598. event->destroy(event);
  1599. put_ctx(event->ctx);
  1600. call_rcu(&event->rcu_head, free_event_rcu);
  1601. }
  1602. int perf_event_release_kernel(struct perf_event *event)
  1603. {
  1604. struct perf_event_context *ctx = event->ctx;
  1605. /*
  1606. * Remove from the PMU, can't get re-enabled since we got
  1607. * here because the last ref went.
  1608. */
  1609. perf_event_disable(event);
  1610. WARN_ON_ONCE(ctx->parent_ctx);
  1611. /*
  1612. * There are two ways this annotation is useful:
  1613. *
  1614. * 1) there is a lock recursion from perf_event_exit_task
  1615. * see the comment there.
  1616. *
  1617. * 2) there is a lock-inversion with mmap_sem through
  1618. * perf_event_read_group(), which takes faults while
  1619. * holding ctx->mutex, however this is called after
  1620. * the last filedesc died, so there is no possibility
  1621. * to trigger the AB-BA case.
  1622. */
  1623. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  1624. raw_spin_lock_irq(&ctx->lock);
  1625. perf_group_detach(event);
  1626. list_del_event(event, ctx);
  1627. raw_spin_unlock_irq(&ctx->lock);
  1628. mutex_unlock(&ctx->mutex);
  1629. mutex_lock(&event->owner->perf_event_mutex);
  1630. list_del_init(&event->owner_entry);
  1631. mutex_unlock(&event->owner->perf_event_mutex);
  1632. put_task_struct(event->owner);
  1633. free_event(event);
  1634. return 0;
  1635. }
  1636. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  1637. /*
  1638. * Called when the last reference to the file is gone.
  1639. */
  1640. static int perf_release(struct inode *inode, struct file *file)
  1641. {
  1642. struct perf_event *event = file->private_data;
  1643. file->private_data = NULL;
  1644. return perf_event_release_kernel(event);
  1645. }
  1646. static int perf_event_read_size(struct perf_event *event)
  1647. {
  1648. int entry = sizeof(u64); /* value */
  1649. int size = 0;
  1650. int nr = 1;
  1651. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1652. size += sizeof(u64);
  1653. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1654. size += sizeof(u64);
  1655. if (event->attr.read_format & PERF_FORMAT_ID)
  1656. entry += sizeof(u64);
  1657. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1658. nr += event->group_leader->nr_siblings;
  1659. size += sizeof(u64);
  1660. }
  1661. size += entry * nr;
  1662. return size;
  1663. }
  1664. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  1665. {
  1666. struct perf_event *child;
  1667. u64 total = 0;
  1668. *enabled = 0;
  1669. *running = 0;
  1670. mutex_lock(&event->child_mutex);
  1671. total += perf_event_read(event);
  1672. *enabled += event->total_time_enabled +
  1673. atomic64_read(&event->child_total_time_enabled);
  1674. *running += event->total_time_running +
  1675. atomic64_read(&event->child_total_time_running);
  1676. list_for_each_entry(child, &event->child_list, child_list) {
  1677. total += perf_event_read(child);
  1678. *enabled += child->total_time_enabled;
  1679. *running += child->total_time_running;
  1680. }
  1681. mutex_unlock(&event->child_mutex);
  1682. return total;
  1683. }
  1684. EXPORT_SYMBOL_GPL(perf_event_read_value);
  1685. static int perf_event_read_group(struct perf_event *event,
  1686. u64 read_format, char __user *buf)
  1687. {
  1688. struct perf_event *leader = event->group_leader, *sub;
  1689. int n = 0, size = 0, ret = -EFAULT;
  1690. struct perf_event_context *ctx = leader->ctx;
  1691. u64 values[5];
  1692. u64 count, enabled, running;
  1693. mutex_lock(&ctx->mutex);
  1694. count = perf_event_read_value(leader, &enabled, &running);
  1695. values[n++] = 1 + leader->nr_siblings;
  1696. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1697. values[n++] = enabled;
  1698. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1699. values[n++] = running;
  1700. values[n++] = count;
  1701. if (read_format & PERF_FORMAT_ID)
  1702. values[n++] = primary_event_id(leader);
  1703. size = n * sizeof(u64);
  1704. if (copy_to_user(buf, values, size))
  1705. goto unlock;
  1706. ret = size;
  1707. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1708. n = 0;
  1709. values[n++] = perf_event_read_value(sub, &enabled, &running);
  1710. if (read_format & PERF_FORMAT_ID)
  1711. values[n++] = primary_event_id(sub);
  1712. size = n * sizeof(u64);
  1713. if (copy_to_user(buf + ret, values, size)) {
  1714. ret = -EFAULT;
  1715. goto unlock;
  1716. }
  1717. ret += size;
  1718. }
  1719. unlock:
  1720. mutex_unlock(&ctx->mutex);
  1721. return ret;
  1722. }
  1723. static int perf_event_read_one(struct perf_event *event,
  1724. u64 read_format, char __user *buf)
  1725. {
  1726. u64 enabled, running;
  1727. u64 values[4];
  1728. int n = 0;
  1729. values[n++] = perf_event_read_value(event, &enabled, &running);
  1730. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1731. values[n++] = enabled;
  1732. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1733. values[n++] = running;
  1734. if (read_format & PERF_FORMAT_ID)
  1735. values[n++] = primary_event_id(event);
  1736. if (copy_to_user(buf, values, n * sizeof(u64)))
  1737. return -EFAULT;
  1738. return n * sizeof(u64);
  1739. }
  1740. /*
  1741. * Read the performance event - simple non blocking version for now
  1742. */
  1743. static ssize_t
  1744. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  1745. {
  1746. u64 read_format = event->attr.read_format;
  1747. int ret;
  1748. /*
  1749. * Return end-of-file for a read on a event that is in
  1750. * error state (i.e. because it was pinned but it couldn't be
  1751. * scheduled on to the CPU at some point).
  1752. */
  1753. if (event->state == PERF_EVENT_STATE_ERROR)
  1754. return 0;
  1755. if (count < perf_event_read_size(event))
  1756. return -ENOSPC;
  1757. WARN_ON_ONCE(event->ctx->parent_ctx);
  1758. if (read_format & PERF_FORMAT_GROUP)
  1759. ret = perf_event_read_group(event, read_format, buf);
  1760. else
  1761. ret = perf_event_read_one(event, read_format, buf);
  1762. return ret;
  1763. }
  1764. static ssize_t
  1765. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1766. {
  1767. struct perf_event *event = file->private_data;
  1768. return perf_read_hw(event, buf, count);
  1769. }
  1770. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1771. {
  1772. struct perf_event *event = file->private_data;
  1773. struct perf_mmap_data *data;
  1774. unsigned int events = POLL_HUP;
  1775. rcu_read_lock();
  1776. data = rcu_dereference(event->data);
  1777. if (data)
  1778. events = atomic_xchg(&data->poll, 0);
  1779. rcu_read_unlock();
  1780. poll_wait(file, &event->waitq, wait);
  1781. return events;
  1782. }
  1783. static void perf_event_reset(struct perf_event *event)
  1784. {
  1785. (void)perf_event_read(event);
  1786. atomic64_set(&event->count, 0);
  1787. perf_event_update_userpage(event);
  1788. }
  1789. /*
  1790. * Holding the top-level event's child_mutex means that any
  1791. * descendant process that has inherited this event will block
  1792. * in sync_child_event if it goes to exit, thus satisfying the
  1793. * task existence requirements of perf_event_enable/disable.
  1794. */
  1795. static void perf_event_for_each_child(struct perf_event *event,
  1796. void (*func)(struct perf_event *))
  1797. {
  1798. struct perf_event *child;
  1799. WARN_ON_ONCE(event->ctx->parent_ctx);
  1800. mutex_lock(&event->child_mutex);
  1801. func(event);
  1802. list_for_each_entry(child, &event->child_list, child_list)
  1803. func(child);
  1804. mutex_unlock(&event->child_mutex);
  1805. }
  1806. static void perf_event_for_each(struct perf_event *event,
  1807. void (*func)(struct perf_event *))
  1808. {
  1809. struct perf_event_context *ctx = event->ctx;
  1810. struct perf_event *sibling;
  1811. WARN_ON_ONCE(ctx->parent_ctx);
  1812. mutex_lock(&ctx->mutex);
  1813. event = event->group_leader;
  1814. perf_event_for_each_child(event, func);
  1815. func(event);
  1816. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  1817. perf_event_for_each_child(event, func);
  1818. mutex_unlock(&ctx->mutex);
  1819. }
  1820. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  1821. {
  1822. struct perf_event_context *ctx = event->ctx;
  1823. unsigned long size;
  1824. int ret = 0;
  1825. u64 value;
  1826. if (!event->attr.sample_period)
  1827. return -EINVAL;
  1828. size = copy_from_user(&value, arg, sizeof(value));
  1829. if (size != sizeof(value))
  1830. return -EFAULT;
  1831. if (!value)
  1832. return -EINVAL;
  1833. raw_spin_lock_irq(&ctx->lock);
  1834. if (event->attr.freq) {
  1835. if (value > sysctl_perf_event_sample_rate) {
  1836. ret = -EINVAL;
  1837. goto unlock;
  1838. }
  1839. event->attr.sample_freq = value;
  1840. } else {
  1841. event->attr.sample_period = value;
  1842. event->hw.sample_period = value;
  1843. }
  1844. unlock:
  1845. raw_spin_unlock_irq(&ctx->lock);
  1846. return ret;
  1847. }
  1848. static const struct file_operations perf_fops;
  1849. static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  1850. {
  1851. struct file *file;
  1852. file = fget_light(fd, fput_needed);
  1853. if (!file)
  1854. return ERR_PTR(-EBADF);
  1855. if (file->f_op != &perf_fops) {
  1856. fput_light(file, *fput_needed);
  1857. *fput_needed = 0;
  1858. return ERR_PTR(-EBADF);
  1859. }
  1860. return file->private_data;
  1861. }
  1862. static int perf_event_set_output(struct perf_event *event,
  1863. struct perf_event *output_event);
  1864. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  1865. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1866. {
  1867. struct perf_event *event = file->private_data;
  1868. void (*func)(struct perf_event *);
  1869. u32 flags = arg;
  1870. switch (cmd) {
  1871. case PERF_EVENT_IOC_ENABLE:
  1872. func = perf_event_enable;
  1873. break;
  1874. case PERF_EVENT_IOC_DISABLE:
  1875. func = perf_event_disable;
  1876. break;
  1877. case PERF_EVENT_IOC_RESET:
  1878. func = perf_event_reset;
  1879. break;
  1880. case PERF_EVENT_IOC_REFRESH:
  1881. return perf_event_refresh(event, arg);
  1882. case PERF_EVENT_IOC_PERIOD:
  1883. return perf_event_period(event, (u64 __user *)arg);
  1884. case PERF_EVENT_IOC_SET_OUTPUT:
  1885. {
  1886. struct perf_event *output_event = NULL;
  1887. int fput_needed = 0;
  1888. int ret;
  1889. if (arg != -1) {
  1890. output_event = perf_fget_light(arg, &fput_needed);
  1891. if (IS_ERR(output_event))
  1892. return PTR_ERR(output_event);
  1893. }
  1894. ret = perf_event_set_output(event, output_event);
  1895. if (output_event)
  1896. fput_light(output_event->filp, fput_needed);
  1897. return ret;
  1898. }
  1899. case PERF_EVENT_IOC_SET_FILTER:
  1900. return perf_event_set_filter(event, (void __user *)arg);
  1901. default:
  1902. return -ENOTTY;
  1903. }
  1904. if (flags & PERF_IOC_FLAG_GROUP)
  1905. perf_event_for_each(event, func);
  1906. else
  1907. perf_event_for_each_child(event, func);
  1908. return 0;
  1909. }
  1910. int perf_event_task_enable(void)
  1911. {
  1912. struct perf_event *event;
  1913. mutex_lock(&current->perf_event_mutex);
  1914. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  1915. perf_event_for_each_child(event, perf_event_enable);
  1916. mutex_unlock(&current->perf_event_mutex);
  1917. return 0;
  1918. }
  1919. int perf_event_task_disable(void)
  1920. {
  1921. struct perf_event *event;
  1922. mutex_lock(&current->perf_event_mutex);
  1923. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  1924. perf_event_for_each_child(event, perf_event_disable);
  1925. mutex_unlock(&current->perf_event_mutex);
  1926. return 0;
  1927. }
  1928. #ifndef PERF_EVENT_INDEX_OFFSET
  1929. # define PERF_EVENT_INDEX_OFFSET 0
  1930. #endif
  1931. static int perf_event_index(struct perf_event *event)
  1932. {
  1933. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1934. return 0;
  1935. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  1936. }
  1937. /*
  1938. * Callers need to ensure there can be no nesting of this function, otherwise
  1939. * the seqlock logic goes bad. We can not serialize this because the arch
  1940. * code calls this from NMI context.
  1941. */
  1942. void perf_event_update_userpage(struct perf_event *event)
  1943. {
  1944. struct perf_event_mmap_page *userpg;
  1945. struct perf_mmap_data *data;
  1946. rcu_read_lock();
  1947. data = rcu_dereference(event->data);
  1948. if (!data)
  1949. goto unlock;
  1950. userpg = data->user_page;
  1951. /*
  1952. * Disable preemption so as to not let the corresponding user-space
  1953. * spin too long if we get preempted.
  1954. */
  1955. preempt_disable();
  1956. ++userpg->lock;
  1957. barrier();
  1958. userpg->index = perf_event_index(event);
  1959. userpg->offset = atomic64_read(&event->count);
  1960. if (event->state == PERF_EVENT_STATE_ACTIVE)
  1961. userpg->offset -= atomic64_read(&event->hw.prev_count);
  1962. userpg->time_enabled = event->total_time_enabled +
  1963. atomic64_read(&event->child_total_time_enabled);
  1964. userpg->time_running = event->total_time_running +
  1965. atomic64_read(&event->child_total_time_running);
  1966. barrier();
  1967. ++userpg->lock;
  1968. preempt_enable();
  1969. unlock:
  1970. rcu_read_unlock();
  1971. }
  1972. #ifndef CONFIG_PERF_USE_VMALLOC
  1973. /*
  1974. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  1975. */
  1976. static struct page *
  1977. perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
  1978. {
  1979. if (pgoff > data->nr_pages)
  1980. return NULL;
  1981. if (pgoff == 0)
  1982. return virt_to_page(data->user_page);
  1983. return virt_to_page(data->data_pages[pgoff - 1]);
  1984. }
  1985. static void *perf_mmap_alloc_page(int cpu)
  1986. {
  1987. struct page *page;
  1988. int node;
  1989. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  1990. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  1991. if (!page)
  1992. return NULL;
  1993. return page_address(page);
  1994. }
  1995. static struct perf_mmap_data *
  1996. perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
  1997. {
  1998. struct perf_mmap_data *data;
  1999. unsigned long size;
  2000. int i;
  2001. size = sizeof(struct perf_mmap_data);
  2002. size += nr_pages * sizeof(void *);
  2003. data = kzalloc(size, GFP_KERNEL);
  2004. if (!data)
  2005. goto fail;
  2006. data->user_page = perf_mmap_alloc_page(event->cpu);
  2007. if (!data->user_page)
  2008. goto fail_user_page;
  2009. for (i = 0; i < nr_pages; i++) {
  2010. data->data_pages[i] = perf_mmap_alloc_page(event->cpu);
  2011. if (!data->data_pages[i])
  2012. goto fail_data_pages;
  2013. }
  2014. data->nr_pages = nr_pages;
  2015. return data;
  2016. fail_data_pages:
  2017. for (i--; i >= 0; i--)
  2018. free_page((unsigned long)data->data_pages[i]);
  2019. free_page((unsigned long)data->user_page);
  2020. fail_user_page:
  2021. kfree(data);
  2022. fail:
  2023. return NULL;
  2024. }
  2025. static void perf_mmap_free_page(unsigned long addr)
  2026. {
  2027. struct page *page = virt_to_page((void *)addr);
  2028. page->mapping = NULL;
  2029. __free_page(page);
  2030. }
  2031. static void perf_mmap_data_free(struct perf_mmap_data *data)
  2032. {
  2033. int i;
  2034. perf_mmap_free_page((unsigned long)data->user_page);
  2035. for (i = 0; i < data->nr_pages; i++)
  2036. perf_mmap_free_page((unsigned long)data->data_pages[i]);
  2037. kfree(data);
  2038. }
  2039. static inline int page_order(struct perf_mmap_data *data)
  2040. {
  2041. return 0;
  2042. }
  2043. #else
  2044. /*
  2045. * Back perf_mmap() with vmalloc memory.
  2046. *
  2047. * Required for architectures that have d-cache aliasing issues.
  2048. */
  2049. static inline int page_order(struct perf_mmap_data *data)
  2050. {
  2051. return data->page_order;
  2052. }
  2053. static struct page *
  2054. perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
  2055. {
  2056. if (pgoff > (1UL << page_order(data)))
  2057. return NULL;
  2058. return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
  2059. }
  2060. static void perf_mmap_unmark_page(void *addr)
  2061. {
  2062. struct page *page = vmalloc_to_page(addr);
  2063. page->mapping = NULL;
  2064. }
  2065. static void perf_mmap_data_free_work(struct work_struct *work)
  2066. {
  2067. struct perf_mmap_data *data;
  2068. void *base;
  2069. int i, nr;
  2070. data = container_of(work, struct perf_mmap_data, work);
  2071. nr = 1 << page_order(data);
  2072. base = data->user_page;
  2073. for (i = 0; i < nr + 1; i++)
  2074. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  2075. vfree(base);
  2076. kfree(data);
  2077. }
  2078. static void perf_mmap_data_free(struct perf_mmap_data *data)
  2079. {
  2080. schedule_work(&data->work);
  2081. }
  2082. static struct perf_mmap_data *
  2083. perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
  2084. {
  2085. struct perf_mmap_data *data;
  2086. unsigned long size;
  2087. void *all_buf;
  2088. size = sizeof(struct perf_mmap_data);
  2089. size += sizeof(void *);
  2090. data = kzalloc(size, GFP_KERNEL);
  2091. if (!data)
  2092. goto fail;
  2093. INIT_WORK(&data->work, perf_mmap_data_free_work);
  2094. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  2095. if (!all_buf)
  2096. goto fail_all_buf;
  2097. data->user_page = all_buf;
  2098. data->data_pages[0] = all_buf + PAGE_SIZE;
  2099. data->page_order = ilog2(nr_pages);
  2100. data->nr_pages = 1;
  2101. return data;
  2102. fail_all_buf:
  2103. kfree(data);
  2104. fail:
  2105. return NULL;
  2106. }
  2107. #endif
  2108. static unsigned long perf_data_size(struct perf_mmap_data *data)
  2109. {
  2110. return data->nr_pages << (PAGE_SHIFT + page_order(data));
  2111. }
  2112. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2113. {
  2114. struct perf_event *event = vma->vm_file->private_data;
  2115. struct perf_mmap_data *data;
  2116. int ret = VM_FAULT_SIGBUS;
  2117. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2118. if (vmf->pgoff == 0)
  2119. ret = 0;
  2120. return ret;
  2121. }
  2122. rcu_read_lock();
  2123. data = rcu_dereference(event->data);
  2124. if (!data)
  2125. goto unlock;
  2126. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2127. goto unlock;
  2128. vmf->page = perf_mmap_to_page(data, vmf->pgoff);
  2129. if (!vmf->page)
  2130. goto unlock;
  2131. get_page(vmf->page);
  2132. vmf->page->mapping = vma->vm_file->f_mapping;
  2133. vmf->page->index = vmf->pgoff;
  2134. ret = 0;
  2135. unlock:
  2136. rcu_read_unlock();
  2137. return ret;
  2138. }
  2139. static void
  2140. perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
  2141. {
  2142. long max_size = perf_data_size(data);
  2143. if (event->attr.watermark) {
  2144. data->watermark = min_t(long, max_size,
  2145. event->attr.wakeup_watermark);
  2146. }
  2147. if (!data->watermark)
  2148. data->watermark = max_size / 2;
  2149. atomic_set(&data->refcount, 1);
  2150. rcu_assign_pointer(event->data, data);
  2151. }
  2152. static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
  2153. {
  2154. struct perf_mmap_data *data;
  2155. data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
  2156. perf_mmap_data_free(data);
  2157. }
  2158. static struct perf_mmap_data *perf_mmap_data_get(struct perf_event *event)
  2159. {
  2160. struct perf_mmap_data *data;
  2161. rcu_read_lock();
  2162. data = rcu_dereference(event->data);
  2163. if (data) {
  2164. if (!atomic_inc_not_zero(&data->refcount))
  2165. data = NULL;
  2166. }
  2167. rcu_read_unlock();
  2168. return data;
  2169. }
  2170. static void perf_mmap_data_put(struct perf_mmap_data *data)
  2171. {
  2172. if (!atomic_dec_and_test(&data->refcount))
  2173. return;
  2174. call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
  2175. }
  2176. static void perf_mmap_open(struct vm_area_struct *vma)
  2177. {
  2178. struct perf_event *event = vma->vm_file->private_data;
  2179. atomic_inc(&event->mmap_count);
  2180. }
  2181. static void perf_mmap_close(struct vm_area_struct *vma)
  2182. {
  2183. struct perf_event *event = vma->vm_file->private_data;
  2184. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2185. unsigned long size = perf_data_size(event->data);
  2186. struct user_struct *user = event->mmap_user;
  2187. struct perf_mmap_data *data = event->data;
  2188. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2189. vma->vm_mm->locked_vm -= event->mmap_locked;
  2190. rcu_assign_pointer(event->data, NULL);
  2191. mutex_unlock(&event->mmap_mutex);
  2192. perf_mmap_data_put(data);
  2193. free_uid(user);
  2194. }
  2195. }
  2196. static const struct vm_operations_struct perf_mmap_vmops = {
  2197. .open = perf_mmap_open,
  2198. .close = perf_mmap_close,
  2199. .fault = perf_mmap_fault,
  2200. .page_mkwrite = perf_mmap_fault,
  2201. };
  2202. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2203. {
  2204. struct perf_event *event = file->private_data;
  2205. unsigned long user_locked, user_lock_limit;
  2206. struct user_struct *user = current_user();
  2207. unsigned long locked, lock_limit;
  2208. struct perf_mmap_data *data;
  2209. unsigned long vma_size;
  2210. unsigned long nr_pages;
  2211. long user_extra, extra;
  2212. int ret = 0;
  2213. /*
  2214. * Don't allow mmap() of inherited per-task counters. This would
  2215. * create a performance issue due to all children writing to the
  2216. * same buffer.
  2217. */
  2218. if (event->cpu == -1 && event->attr.inherit)
  2219. return -EINVAL;
  2220. if (!(vma->vm_flags & VM_SHARED))
  2221. return -EINVAL;
  2222. vma_size = vma->vm_end - vma->vm_start;
  2223. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2224. /*
  2225. * If we have data pages ensure they're a power-of-two number, so we
  2226. * can do bitmasks instead of modulo.
  2227. */
  2228. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2229. return -EINVAL;
  2230. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2231. return -EINVAL;
  2232. if (vma->vm_pgoff != 0)
  2233. return -EINVAL;
  2234. WARN_ON_ONCE(event->ctx->parent_ctx);
  2235. mutex_lock(&event->mmap_mutex);
  2236. if (event->data) {
  2237. if (event->data->nr_pages == nr_pages)
  2238. atomic_inc(&event->data->refcount);
  2239. else
  2240. ret = -EINVAL;
  2241. goto unlock;
  2242. }
  2243. user_extra = nr_pages + 1;
  2244. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2245. /*
  2246. * Increase the limit linearly with more CPUs:
  2247. */
  2248. user_lock_limit *= num_online_cpus();
  2249. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2250. extra = 0;
  2251. if (user_locked > user_lock_limit)
  2252. extra = user_locked - user_lock_limit;
  2253. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2254. lock_limit >>= PAGE_SHIFT;
  2255. locked = vma->vm_mm->locked_vm + extra;
  2256. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2257. !capable(CAP_IPC_LOCK)) {
  2258. ret = -EPERM;
  2259. goto unlock;
  2260. }
  2261. WARN_ON(event->data);
  2262. data = perf_mmap_data_alloc(event, nr_pages);
  2263. if (!data) {
  2264. ret = -ENOMEM;
  2265. goto unlock;
  2266. }
  2267. perf_mmap_data_init(event, data);
  2268. if (vma->vm_flags & VM_WRITE)
  2269. event->data->writable = 1;
  2270. atomic_long_add(user_extra, &user->locked_vm);
  2271. event->mmap_locked = extra;
  2272. event->mmap_user = get_current_user();
  2273. vma->vm_mm->locked_vm += event->mmap_locked;
  2274. unlock:
  2275. if (!ret)
  2276. atomic_inc(&event->mmap_count);
  2277. mutex_unlock(&event->mmap_mutex);
  2278. vma->vm_flags |= VM_RESERVED;
  2279. vma->vm_ops = &perf_mmap_vmops;
  2280. return ret;
  2281. }
  2282. static int perf_fasync(int fd, struct file *filp, int on)
  2283. {
  2284. struct inode *inode = filp->f_path.dentry->d_inode;
  2285. struct perf_event *event = filp->private_data;
  2286. int retval;
  2287. mutex_lock(&inode->i_mutex);
  2288. retval = fasync_helper(fd, filp, on, &event->fasync);
  2289. mutex_unlock(&inode->i_mutex);
  2290. if (retval < 0)
  2291. return retval;
  2292. return 0;
  2293. }
  2294. static const struct file_operations perf_fops = {
  2295. .llseek = no_llseek,
  2296. .release = perf_release,
  2297. .read = perf_read,
  2298. .poll = perf_poll,
  2299. .unlocked_ioctl = perf_ioctl,
  2300. .compat_ioctl = perf_ioctl,
  2301. .mmap = perf_mmap,
  2302. .fasync = perf_fasync,
  2303. };
  2304. /*
  2305. * Perf event wakeup
  2306. *
  2307. * If there's data, ensure we set the poll() state and publish everything
  2308. * to user-space before waking everybody up.
  2309. */
  2310. void perf_event_wakeup(struct perf_event *event)
  2311. {
  2312. wake_up_all(&event->waitq);
  2313. if (event->pending_kill) {
  2314. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2315. event->pending_kill = 0;
  2316. }
  2317. }
  2318. /*
  2319. * Pending wakeups
  2320. *
  2321. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  2322. *
  2323. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  2324. * single linked list and use cmpxchg() to add entries lockless.
  2325. */
  2326. static void perf_pending_event(struct perf_pending_entry *entry)
  2327. {
  2328. struct perf_event *event = container_of(entry,
  2329. struct perf_event, pending);
  2330. if (event->pending_disable) {
  2331. event->pending_disable = 0;
  2332. __perf_event_disable(event);
  2333. }
  2334. if (event->pending_wakeup) {
  2335. event->pending_wakeup = 0;
  2336. perf_event_wakeup(event);
  2337. }
  2338. }
  2339. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  2340. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  2341. PENDING_TAIL,
  2342. };
  2343. static void perf_pending_queue(struct perf_pending_entry *entry,
  2344. void (*func)(struct perf_pending_entry *))
  2345. {
  2346. struct perf_pending_entry **head;
  2347. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  2348. return;
  2349. entry->func = func;
  2350. head = &get_cpu_var(perf_pending_head);
  2351. do {
  2352. entry->next = *head;
  2353. } while (cmpxchg(head, entry->next, entry) != entry->next);
  2354. set_perf_event_pending();
  2355. put_cpu_var(perf_pending_head);
  2356. }
  2357. static int __perf_pending_run(void)
  2358. {
  2359. struct perf_pending_entry *list;
  2360. int nr = 0;
  2361. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  2362. while (list != PENDING_TAIL) {
  2363. void (*func)(struct perf_pending_entry *);
  2364. struct perf_pending_entry *entry = list;
  2365. list = list->next;
  2366. func = entry->func;
  2367. entry->next = NULL;
  2368. /*
  2369. * Ensure we observe the unqueue before we issue the wakeup,
  2370. * so that we won't be waiting forever.
  2371. * -- see perf_not_pending().
  2372. */
  2373. smp_wmb();
  2374. func(entry);
  2375. nr++;
  2376. }
  2377. return nr;
  2378. }
  2379. static inline int perf_not_pending(struct perf_event *event)
  2380. {
  2381. /*
  2382. * If we flush on whatever cpu we run, there is a chance we don't
  2383. * need to wait.
  2384. */
  2385. get_cpu();
  2386. __perf_pending_run();
  2387. put_cpu();
  2388. /*
  2389. * Ensure we see the proper queue state before going to sleep
  2390. * so that we do not miss the wakeup. -- see perf_pending_handle()
  2391. */
  2392. smp_rmb();
  2393. return event->pending.next == NULL;
  2394. }
  2395. static void perf_pending_sync(struct perf_event *event)
  2396. {
  2397. wait_event(event->waitq, perf_not_pending(event));
  2398. }
  2399. void perf_event_do_pending(void)
  2400. {
  2401. __perf_pending_run();
  2402. }
  2403. /*
  2404. * Callchain support -- arch specific
  2405. */
  2406. __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  2407. {
  2408. return NULL;
  2409. }
  2410. __weak
  2411. void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int skip)
  2412. {
  2413. }
  2414. /*
  2415. * We assume there is only KVM supporting the callbacks.
  2416. * Later on, we might change it to a list if there is
  2417. * another virtualization implementation supporting the callbacks.
  2418. */
  2419. struct perf_guest_info_callbacks *perf_guest_cbs;
  2420. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2421. {
  2422. perf_guest_cbs = cbs;
  2423. return 0;
  2424. }
  2425. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  2426. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2427. {
  2428. perf_guest_cbs = NULL;
  2429. return 0;
  2430. }
  2431. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  2432. /*
  2433. * Output
  2434. */
  2435. static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
  2436. unsigned long offset, unsigned long head)
  2437. {
  2438. unsigned long mask;
  2439. if (!data->writable)
  2440. return true;
  2441. mask = perf_data_size(data) - 1;
  2442. offset = (offset - tail) & mask;
  2443. head = (head - tail) & mask;
  2444. if ((int)(head - offset) < 0)
  2445. return false;
  2446. return true;
  2447. }
  2448. static void perf_output_wakeup(struct perf_output_handle *handle)
  2449. {
  2450. atomic_set(&handle->data->poll, POLL_IN);
  2451. if (handle->nmi) {
  2452. handle->event->pending_wakeup = 1;
  2453. perf_pending_queue(&handle->event->pending,
  2454. perf_pending_event);
  2455. } else
  2456. perf_event_wakeup(handle->event);
  2457. }
  2458. /*
  2459. * We need to ensure a later event_id doesn't publish a head when a former
  2460. * event isn't done writing. However since we need to deal with NMIs we
  2461. * cannot fully serialize things.
  2462. *
  2463. * We only publish the head (and generate a wakeup) when the outer-most
  2464. * event completes.
  2465. */
  2466. static void perf_output_get_handle(struct perf_output_handle *handle)
  2467. {
  2468. struct perf_mmap_data *data = handle->data;
  2469. preempt_disable();
  2470. local_inc(&data->nest);
  2471. handle->wakeup = local_read(&data->wakeup);
  2472. }
  2473. static void perf_output_put_handle(struct perf_output_handle *handle)
  2474. {
  2475. struct perf_mmap_data *data = handle->data;
  2476. unsigned long head;
  2477. again:
  2478. head = local_read(&data->head);
  2479. /*
  2480. * IRQ/NMI can happen here, which means we can miss a head update.
  2481. */
  2482. if (!local_dec_and_test(&data->nest))
  2483. goto out;
  2484. /*
  2485. * Publish the known good head. Rely on the full barrier implied
  2486. * by atomic_dec_and_test() order the data->head read and this
  2487. * write.
  2488. */
  2489. data->user_page->data_head = head;
  2490. /*
  2491. * Now check if we missed an update, rely on the (compiler)
  2492. * barrier in atomic_dec_and_test() to re-read data->head.
  2493. */
  2494. if (unlikely(head != local_read(&data->head))) {
  2495. local_inc(&data->nest);
  2496. goto again;
  2497. }
  2498. if (handle->wakeup != local_read(&data->wakeup))
  2499. perf_output_wakeup(handle);
  2500. out:
  2501. preempt_enable();
  2502. }
  2503. __always_inline void perf_output_copy(struct perf_output_handle *handle,
  2504. const void *buf, unsigned int len)
  2505. {
  2506. do {
  2507. unsigned long size = min_t(unsigned long, handle->size, len);
  2508. memcpy(handle->addr, buf, size);
  2509. len -= size;
  2510. handle->addr += size;
  2511. buf += size;
  2512. handle->size -= size;
  2513. if (!handle->size) {
  2514. struct perf_mmap_data *data = handle->data;
  2515. handle->page++;
  2516. handle->page &= data->nr_pages - 1;
  2517. handle->addr = data->data_pages[handle->page];
  2518. handle->size = PAGE_SIZE << page_order(data);
  2519. }
  2520. } while (len);
  2521. }
  2522. int perf_output_begin(struct perf_output_handle *handle,
  2523. struct perf_event *event, unsigned int size,
  2524. int nmi, int sample)
  2525. {
  2526. struct perf_mmap_data *data;
  2527. unsigned long tail, offset, head;
  2528. int have_lost;
  2529. struct {
  2530. struct perf_event_header header;
  2531. u64 id;
  2532. u64 lost;
  2533. } lost_event;
  2534. rcu_read_lock();
  2535. /*
  2536. * For inherited events we send all the output towards the parent.
  2537. */
  2538. if (event->parent)
  2539. event = event->parent;
  2540. data = rcu_dereference(event->data);
  2541. if (!data)
  2542. goto out;
  2543. handle->data = data;
  2544. handle->event = event;
  2545. handle->nmi = nmi;
  2546. handle->sample = sample;
  2547. if (!data->nr_pages)
  2548. goto out;
  2549. have_lost = local_read(&data->lost);
  2550. if (have_lost)
  2551. size += sizeof(lost_event);
  2552. perf_output_get_handle(handle);
  2553. do {
  2554. /*
  2555. * Userspace could choose to issue a mb() before updating the
  2556. * tail pointer. So that all reads will be completed before the
  2557. * write is issued.
  2558. */
  2559. tail = ACCESS_ONCE(data->user_page->data_tail);
  2560. smp_rmb();
  2561. offset = head = local_read(&data->head);
  2562. head += size;
  2563. if (unlikely(!perf_output_space(data, tail, offset, head)))
  2564. goto fail;
  2565. } while (local_cmpxchg(&data->head, offset, head) != offset);
  2566. if (head - local_read(&data->wakeup) > data->watermark)
  2567. local_add(data->watermark, &data->wakeup);
  2568. handle->page = offset >> (PAGE_SHIFT + page_order(data));
  2569. handle->page &= data->nr_pages - 1;
  2570. handle->size = offset & ((PAGE_SIZE << page_order(data)) - 1);
  2571. handle->addr = data->data_pages[handle->page];
  2572. handle->addr += handle->size;
  2573. handle->size = (PAGE_SIZE << page_order(data)) - handle->size;
  2574. if (have_lost) {
  2575. lost_event.header.type = PERF_RECORD_LOST;
  2576. lost_event.header.misc = 0;
  2577. lost_event.header.size = sizeof(lost_event);
  2578. lost_event.id = event->id;
  2579. lost_event.lost = local_xchg(&data->lost, 0);
  2580. perf_output_put(handle, lost_event);
  2581. }
  2582. return 0;
  2583. fail:
  2584. local_inc(&data->lost);
  2585. perf_output_put_handle(handle);
  2586. out:
  2587. rcu_read_unlock();
  2588. return -ENOSPC;
  2589. }
  2590. void perf_output_end(struct perf_output_handle *handle)
  2591. {
  2592. struct perf_event *event = handle->event;
  2593. struct perf_mmap_data *data = handle->data;
  2594. int wakeup_events = event->attr.wakeup_events;
  2595. if (handle->sample && wakeup_events) {
  2596. int events = local_inc_return(&data->events);
  2597. if (events >= wakeup_events) {
  2598. local_sub(wakeup_events, &data->events);
  2599. local_inc(&data->wakeup);
  2600. }
  2601. }
  2602. perf_output_put_handle(handle);
  2603. rcu_read_unlock();
  2604. }
  2605. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  2606. {
  2607. /*
  2608. * only top level events have the pid namespace they were created in
  2609. */
  2610. if (event->parent)
  2611. event = event->parent;
  2612. return task_tgid_nr_ns(p, event->ns);
  2613. }
  2614. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  2615. {
  2616. /*
  2617. * only top level events have the pid namespace they were created in
  2618. */
  2619. if (event->parent)
  2620. event = event->parent;
  2621. return task_pid_nr_ns(p, event->ns);
  2622. }
  2623. static void perf_output_read_one(struct perf_output_handle *handle,
  2624. struct perf_event *event)
  2625. {
  2626. u64 read_format = event->attr.read_format;
  2627. u64 values[4];
  2628. int n = 0;
  2629. values[n++] = atomic64_read(&event->count);
  2630. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2631. values[n++] = event->total_time_enabled +
  2632. atomic64_read(&event->child_total_time_enabled);
  2633. }
  2634. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2635. values[n++] = event->total_time_running +
  2636. atomic64_read(&event->child_total_time_running);
  2637. }
  2638. if (read_format & PERF_FORMAT_ID)
  2639. values[n++] = primary_event_id(event);
  2640. perf_output_copy(handle, values, n * sizeof(u64));
  2641. }
  2642. /*
  2643. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2644. */
  2645. static void perf_output_read_group(struct perf_output_handle *handle,
  2646. struct perf_event *event)
  2647. {
  2648. struct perf_event *leader = event->group_leader, *sub;
  2649. u64 read_format = event->attr.read_format;
  2650. u64 values[5];
  2651. int n = 0;
  2652. values[n++] = 1 + leader->nr_siblings;
  2653. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2654. values[n++] = leader->total_time_enabled;
  2655. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2656. values[n++] = leader->total_time_running;
  2657. if (leader != event)
  2658. leader->pmu->read(leader);
  2659. values[n++] = atomic64_read(&leader->count);
  2660. if (read_format & PERF_FORMAT_ID)
  2661. values[n++] = primary_event_id(leader);
  2662. perf_output_copy(handle, values, n * sizeof(u64));
  2663. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2664. n = 0;
  2665. if (sub != event)
  2666. sub->pmu->read(sub);
  2667. values[n++] = atomic64_read(&sub->count);
  2668. if (read_format & PERF_FORMAT_ID)
  2669. values[n++] = primary_event_id(sub);
  2670. perf_output_copy(handle, values, n * sizeof(u64));
  2671. }
  2672. }
  2673. static void perf_output_read(struct perf_output_handle *handle,
  2674. struct perf_event *event)
  2675. {
  2676. if (event->attr.read_format & PERF_FORMAT_GROUP)
  2677. perf_output_read_group(handle, event);
  2678. else
  2679. perf_output_read_one(handle, event);
  2680. }
  2681. void perf_output_sample(struct perf_output_handle *handle,
  2682. struct perf_event_header *header,
  2683. struct perf_sample_data *data,
  2684. struct perf_event *event)
  2685. {
  2686. u64 sample_type = data->type;
  2687. perf_output_put(handle, *header);
  2688. if (sample_type & PERF_SAMPLE_IP)
  2689. perf_output_put(handle, data->ip);
  2690. if (sample_type & PERF_SAMPLE_TID)
  2691. perf_output_put(handle, data->tid_entry);
  2692. if (sample_type & PERF_SAMPLE_TIME)
  2693. perf_output_put(handle, data->time);
  2694. if (sample_type & PERF_SAMPLE_ADDR)
  2695. perf_output_put(handle, data->addr);
  2696. if (sample_type & PERF_SAMPLE_ID)
  2697. perf_output_put(handle, data->id);
  2698. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2699. perf_output_put(handle, data->stream_id);
  2700. if (sample_type & PERF_SAMPLE_CPU)
  2701. perf_output_put(handle, data->cpu_entry);
  2702. if (sample_type & PERF_SAMPLE_PERIOD)
  2703. perf_output_put(handle, data->period);
  2704. if (sample_type & PERF_SAMPLE_READ)
  2705. perf_output_read(handle, event);
  2706. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2707. if (data->callchain) {
  2708. int size = 1;
  2709. if (data->callchain)
  2710. size += data->callchain->nr;
  2711. size *= sizeof(u64);
  2712. perf_output_copy(handle, data->callchain, size);
  2713. } else {
  2714. u64 nr = 0;
  2715. perf_output_put(handle, nr);
  2716. }
  2717. }
  2718. if (sample_type & PERF_SAMPLE_RAW) {
  2719. if (data->raw) {
  2720. perf_output_put(handle, data->raw->size);
  2721. perf_output_copy(handle, data->raw->data,
  2722. data->raw->size);
  2723. } else {
  2724. struct {
  2725. u32 size;
  2726. u32 data;
  2727. } raw = {
  2728. .size = sizeof(u32),
  2729. .data = 0,
  2730. };
  2731. perf_output_put(handle, raw);
  2732. }
  2733. }
  2734. }
  2735. void perf_prepare_sample(struct perf_event_header *header,
  2736. struct perf_sample_data *data,
  2737. struct perf_event *event,
  2738. struct pt_regs *regs)
  2739. {
  2740. u64 sample_type = event->attr.sample_type;
  2741. data->type = sample_type;
  2742. header->type = PERF_RECORD_SAMPLE;
  2743. header->size = sizeof(*header);
  2744. header->misc = 0;
  2745. header->misc |= perf_misc_flags(regs);
  2746. if (sample_type & PERF_SAMPLE_IP) {
  2747. data->ip = perf_instruction_pointer(regs);
  2748. header->size += sizeof(data->ip);
  2749. }
  2750. if (sample_type & PERF_SAMPLE_TID) {
  2751. /* namespace issues */
  2752. data->tid_entry.pid = perf_event_pid(event, current);
  2753. data->tid_entry.tid = perf_event_tid(event, current);
  2754. header->size += sizeof(data->tid_entry);
  2755. }
  2756. if (sample_type & PERF_SAMPLE_TIME) {
  2757. data->time = perf_clock();
  2758. header->size += sizeof(data->time);
  2759. }
  2760. if (sample_type & PERF_SAMPLE_ADDR)
  2761. header->size += sizeof(data->addr);
  2762. if (sample_type & PERF_SAMPLE_ID) {
  2763. data->id = primary_event_id(event);
  2764. header->size += sizeof(data->id);
  2765. }
  2766. if (sample_type & PERF_SAMPLE_STREAM_ID) {
  2767. data->stream_id = event->id;
  2768. header->size += sizeof(data->stream_id);
  2769. }
  2770. if (sample_type & PERF_SAMPLE_CPU) {
  2771. data->cpu_entry.cpu = raw_smp_processor_id();
  2772. data->cpu_entry.reserved = 0;
  2773. header->size += sizeof(data->cpu_entry);
  2774. }
  2775. if (sample_type & PERF_SAMPLE_PERIOD)
  2776. header->size += sizeof(data->period);
  2777. if (sample_type & PERF_SAMPLE_READ)
  2778. header->size += perf_event_read_size(event);
  2779. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2780. int size = 1;
  2781. data->callchain = perf_callchain(regs);
  2782. if (data->callchain)
  2783. size += data->callchain->nr;
  2784. header->size += size * sizeof(u64);
  2785. }
  2786. if (sample_type & PERF_SAMPLE_RAW) {
  2787. int size = sizeof(u32);
  2788. if (data->raw)
  2789. size += data->raw->size;
  2790. else
  2791. size += sizeof(u32);
  2792. WARN_ON_ONCE(size & (sizeof(u64)-1));
  2793. header->size += size;
  2794. }
  2795. }
  2796. static void perf_event_output(struct perf_event *event, int nmi,
  2797. struct perf_sample_data *data,
  2798. struct pt_regs *regs)
  2799. {
  2800. struct perf_output_handle handle;
  2801. struct perf_event_header header;
  2802. perf_prepare_sample(&header, data, event, regs);
  2803. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  2804. return;
  2805. perf_output_sample(&handle, &header, data, event);
  2806. perf_output_end(&handle);
  2807. }
  2808. /*
  2809. * read event_id
  2810. */
  2811. struct perf_read_event {
  2812. struct perf_event_header header;
  2813. u32 pid;
  2814. u32 tid;
  2815. };
  2816. static void
  2817. perf_event_read_event(struct perf_event *event,
  2818. struct task_struct *task)
  2819. {
  2820. struct perf_output_handle handle;
  2821. struct perf_read_event read_event = {
  2822. .header = {
  2823. .type = PERF_RECORD_READ,
  2824. .misc = 0,
  2825. .size = sizeof(read_event) + perf_event_read_size(event),
  2826. },
  2827. .pid = perf_event_pid(event, task),
  2828. .tid = perf_event_tid(event, task),
  2829. };
  2830. int ret;
  2831. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  2832. if (ret)
  2833. return;
  2834. perf_output_put(&handle, read_event);
  2835. perf_output_read(&handle, event);
  2836. perf_output_end(&handle);
  2837. }
  2838. /*
  2839. * task tracking -- fork/exit
  2840. *
  2841. * enabled by: attr.comm | attr.mmap | attr.task
  2842. */
  2843. struct perf_task_event {
  2844. struct task_struct *task;
  2845. struct perf_event_context *task_ctx;
  2846. struct {
  2847. struct perf_event_header header;
  2848. u32 pid;
  2849. u32 ppid;
  2850. u32 tid;
  2851. u32 ptid;
  2852. u64 time;
  2853. } event_id;
  2854. };
  2855. static void perf_event_task_output(struct perf_event *event,
  2856. struct perf_task_event *task_event)
  2857. {
  2858. struct perf_output_handle handle;
  2859. struct task_struct *task = task_event->task;
  2860. int size, ret;
  2861. size = task_event->event_id.header.size;
  2862. ret = perf_output_begin(&handle, event, size, 0, 0);
  2863. if (ret)
  2864. return;
  2865. task_event->event_id.pid = perf_event_pid(event, task);
  2866. task_event->event_id.ppid = perf_event_pid(event, current);
  2867. task_event->event_id.tid = perf_event_tid(event, task);
  2868. task_event->event_id.ptid = perf_event_tid(event, current);
  2869. perf_output_put(&handle, task_event->event_id);
  2870. perf_output_end(&handle);
  2871. }
  2872. static int perf_event_task_match(struct perf_event *event)
  2873. {
  2874. if (event->state < PERF_EVENT_STATE_INACTIVE)
  2875. return 0;
  2876. if (event->cpu != -1 && event->cpu != smp_processor_id())
  2877. return 0;
  2878. if (event->attr.comm || event->attr.mmap || event->attr.task)
  2879. return 1;
  2880. return 0;
  2881. }
  2882. static void perf_event_task_ctx(struct perf_event_context *ctx,
  2883. struct perf_task_event *task_event)
  2884. {
  2885. struct perf_event *event;
  2886. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2887. if (perf_event_task_match(event))
  2888. perf_event_task_output(event, task_event);
  2889. }
  2890. }
  2891. static void perf_event_task_event(struct perf_task_event *task_event)
  2892. {
  2893. struct perf_cpu_context *cpuctx;
  2894. struct perf_event_context *ctx = task_event->task_ctx;
  2895. rcu_read_lock();
  2896. cpuctx = &get_cpu_var(perf_cpu_context);
  2897. perf_event_task_ctx(&cpuctx->ctx, task_event);
  2898. if (!ctx)
  2899. ctx = rcu_dereference(current->perf_event_ctxp);
  2900. if (ctx)
  2901. perf_event_task_ctx(ctx, task_event);
  2902. put_cpu_var(perf_cpu_context);
  2903. rcu_read_unlock();
  2904. }
  2905. static void perf_event_task(struct task_struct *task,
  2906. struct perf_event_context *task_ctx,
  2907. int new)
  2908. {
  2909. struct perf_task_event task_event;
  2910. if (!atomic_read(&nr_comm_events) &&
  2911. !atomic_read(&nr_mmap_events) &&
  2912. !atomic_read(&nr_task_events))
  2913. return;
  2914. task_event = (struct perf_task_event){
  2915. .task = task,
  2916. .task_ctx = task_ctx,
  2917. .event_id = {
  2918. .header = {
  2919. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  2920. .misc = 0,
  2921. .size = sizeof(task_event.event_id),
  2922. },
  2923. /* .pid */
  2924. /* .ppid */
  2925. /* .tid */
  2926. /* .ptid */
  2927. .time = perf_clock(),
  2928. },
  2929. };
  2930. perf_event_task_event(&task_event);
  2931. }
  2932. void perf_event_fork(struct task_struct *task)
  2933. {
  2934. perf_event_task(task, NULL, 1);
  2935. }
  2936. /*
  2937. * comm tracking
  2938. */
  2939. struct perf_comm_event {
  2940. struct task_struct *task;
  2941. char *comm;
  2942. int comm_size;
  2943. struct {
  2944. struct perf_event_header header;
  2945. u32 pid;
  2946. u32 tid;
  2947. } event_id;
  2948. };
  2949. static void perf_event_comm_output(struct perf_event *event,
  2950. struct perf_comm_event *comm_event)
  2951. {
  2952. struct perf_output_handle handle;
  2953. int size = comm_event->event_id.header.size;
  2954. int ret = perf_output_begin(&handle, event, size, 0, 0);
  2955. if (ret)
  2956. return;
  2957. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  2958. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  2959. perf_output_put(&handle, comm_event->event_id);
  2960. perf_output_copy(&handle, comm_event->comm,
  2961. comm_event->comm_size);
  2962. perf_output_end(&handle);
  2963. }
  2964. static int perf_event_comm_match(struct perf_event *event)
  2965. {
  2966. if (event->state < PERF_EVENT_STATE_INACTIVE)
  2967. return 0;
  2968. if (event->cpu != -1 && event->cpu != smp_processor_id())
  2969. return 0;
  2970. if (event->attr.comm)
  2971. return 1;
  2972. return 0;
  2973. }
  2974. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  2975. struct perf_comm_event *comm_event)
  2976. {
  2977. struct perf_event *event;
  2978. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2979. if (perf_event_comm_match(event))
  2980. perf_event_comm_output(event, comm_event);
  2981. }
  2982. }
  2983. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  2984. {
  2985. struct perf_cpu_context *cpuctx;
  2986. struct perf_event_context *ctx;
  2987. unsigned int size;
  2988. char comm[TASK_COMM_LEN];
  2989. memset(comm, 0, sizeof(comm));
  2990. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  2991. size = ALIGN(strlen(comm)+1, sizeof(u64));
  2992. comm_event->comm = comm;
  2993. comm_event->comm_size = size;
  2994. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  2995. rcu_read_lock();
  2996. cpuctx = &get_cpu_var(perf_cpu_context);
  2997. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  2998. ctx = rcu_dereference(current->perf_event_ctxp);
  2999. if (ctx)
  3000. perf_event_comm_ctx(ctx, comm_event);
  3001. put_cpu_var(perf_cpu_context);
  3002. rcu_read_unlock();
  3003. }
  3004. void perf_event_comm(struct task_struct *task)
  3005. {
  3006. struct perf_comm_event comm_event;
  3007. if (task->perf_event_ctxp)
  3008. perf_event_enable_on_exec(task);
  3009. if (!atomic_read(&nr_comm_events))
  3010. return;
  3011. comm_event = (struct perf_comm_event){
  3012. .task = task,
  3013. /* .comm */
  3014. /* .comm_size */
  3015. .event_id = {
  3016. .header = {
  3017. .type = PERF_RECORD_COMM,
  3018. .misc = 0,
  3019. /* .size */
  3020. },
  3021. /* .pid */
  3022. /* .tid */
  3023. },
  3024. };
  3025. perf_event_comm_event(&comm_event);
  3026. }
  3027. /*
  3028. * mmap tracking
  3029. */
  3030. struct perf_mmap_event {
  3031. struct vm_area_struct *vma;
  3032. const char *file_name;
  3033. int file_size;
  3034. struct {
  3035. struct perf_event_header header;
  3036. u32 pid;
  3037. u32 tid;
  3038. u64 start;
  3039. u64 len;
  3040. u64 pgoff;
  3041. } event_id;
  3042. };
  3043. static void perf_event_mmap_output(struct perf_event *event,
  3044. struct perf_mmap_event *mmap_event)
  3045. {
  3046. struct perf_output_handle handle;
  3047. int size = mmap_event->event_id.header.size;
  3048. int ret = perf_output_begin(&handle, event, size, 0, 0);
  3049. if (ret)
  3050. return;
  3051. mmap_event->event_id.pid = perf_event_pid(event, current);
  3052. mmap_event->event_id.tid = perf_event_tid(event, current);
  3053. perf_output_put(&handle, mmap_event->event_id);
  3054. perf_output_copy(&handle, mmap_event->file_name,
  3055. mmap_event->file_size);
  3056. perf_output_end(&handle);
  3057. }
  3058. static int perf_event_mmap_match(struct perf_event *event,
  3059. struct perf_mmap_event *mmap_event)
  3060. {
  3061. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3062. return 0;
  3063. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3064. return 0;
  3065. if (event->attr.mmap)
  3066. return 1;
  3067. return 0;
  3068. }
  3069. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3070. struct perf_mmap_event *mmap_event)
  3071. {
  3072. struct perf_event *event;
  3073. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3074. if (perf_event_mmap_match(event, mmap_event))
  3075. perf_event_mmap_output(event, mmap_event);
  3076. }
  3077. }
  3078. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3079. {
  3080. struct perf_cpu_context *cpuctx;
  3081. struct perf_event_context *ctx;
  3082. struct vm_area_struct *vma = mmap_event->vma;
  3083. struct file *file = vma->vm_file;
  3084. unsigned int size;
  3085. char tmp[16];
  3086. char *buf = NULL;
  3087. const char *name;
  3088. memset(tmp, 0, sizeof(tmp));
  3089. if (file) {
  3090. /*
  3091. * d_path works from the end of the buffer backwards, so we
  3092. * need to add enough zero bytes after the string to handle
  3093. * the 64bit alignment we do later.
  3094. */
  3095. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3096. if (!buf) {
  3097. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3098. goto got_name;
  3099. }
  3100. name = d_path(&file->f_path, buf, PATH_MAX);
  3101. if (IS_ERR(name)) {
  3102. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3103. goto got_name;
  3104. }
  3105. } else {
  3106. if (arch_vma_name(mmap_event->vma)) {
  3107. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3108. sizeof(tmp));
  3109. goto got_name;
  3110. }
  3111. if (!vma->vm_mm) {
  3112. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3113. goto got_name;
  3114. }
  3115. name = strncpy(tmp, "//anon", sizeof(tmp));
  3116. goto got_name;
  3117. }
  3118. got_name:
  3119. size = ALIGN(strlen(name)+1, sizeof(u64));
  3120. mmap_event->file_name = name;
  3121. mmap_event->file_size = size;
  3122. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3123. rcu_read_lock();
  3124. cpuctx = &get_cpu_var(perf_cpu_context);
  3125. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
  3126. ctx = rcu_dereference(current->perf_event_ctxp);
  3127. if (ctx)
  3128. perf_event_mmap_ctx(ctx, mmap_event);
  3129. put_cpu_var(perf_cpu_context);
  3130. rcu_read_unlock();
  3131. kfree(buf);
  3132. }
  3133. void __perf_event_mmap(struct vm_area_struct *vma)
  3134. {
  3135. struct perf_mmap_event mmap_event;
  3136. if (!atomic_read(&nr_mmap_events))
  3137. return;
  3138. mmap_event = (struct perf_mmap_event){
  3139. .vma = vma,
  3140. /* .file_name */
  3141. /* .file_size */
  3142. .event_id = {
  3143. .header = {
  3144. .type = PERF_RECORD_MMAP,
  3145. .misc = PERF_RECORD_MISC_USER,
  3146. /* .size */
  3147. },
  3148. /* .pid */
  3149. /* .tid */
  3150. .start = vma->vm_start,
  3151. .len = vma->vm_end - vma->vm_start,
  3152. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3153. },
  3154. };
  3155. perf_event_mmap_event(&mmap_event);
  3156. }
  3157. /*
  3158. * IRQ throttle logging
  3159. */
  3160. static void perf_log_throttle(struct perf_event *event, int enable)
  3161. {
  3162. struct perf_output_handle handle;
  3163. int ret;
  3164. struct {
  3165. struct perf_event_header header;
  3166. u64 time;
  3167. u64 id;
  3168. u64 stream_id;
  3169. } throttle_event = {
  3170. .header = {
  3171. .type = PERF_RECORD_THROTTLE,
  3172. .misc = 0,
  3173. .size = sizeof(throttle_event),
  3174. },
  3175. .time = perf_clock(),
  3176. .id = primary_event_id(event),
  3177. .stream_id = event->id,
  3178. };
  3179. if (enable)
  3180. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3181. ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
  3182. if (ret)
  3183. return;
  3184. perf_output_put(&handle, throttle_event);
  3185. perf_output_end(&handle);
  3186. }
  3187. /*
  3188. * Generic event overflow handling, sampling.
  3189. */
  3190. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3191. int throttle, struct perf_sample_data *data,
  3192. struct pt_regs *regs)
  3193. {
  3194. int events = atomic_read(&event->event_limit);
  3195. struct hw_perf_event *hwc = &event->hw;
  3196. int ret = 0;
  3197. throttle = (throttle && event->pmu->unthrottle != NULL);
  3198. if (!throttle) {
  3199. hwc->interrupts++;
  3200. } else {
  3201. if (hwc->interrupts != MAX_INTERRUPTS) {
  3202. hwc->interrupts++;
  3203. if (HZ * hwc->interrupts >
  3204. (u64)sysctl_perf_event_sample_rate) {
  3205. hwc->interrupts = MAX_INTERRUPTS;
  3206. perf_log_throttle(event, 0);
  3207. ret = 1;
  3208. }
  3209. } else {
  3210. /*
  3211. * Keep re-disabling events even though on the previous
  3212. * pass we disabled it - just in case we raced with a
  3213. * sched-in and the event got enabled again:
  3214. */
  3215. ret = 1;
  3216. }
  3217. }
  3218. if (event->attr.freq) {
  3219. u64 now = perf_clock();
  3220. s64 delta = now - hwc->freq_time_stamp;
  3221. hwc->freq_time_stamp = now;
  3222. if (delta > 0 && delta < 2*TICK_NSEC)
  3223. perf_adjust_period(event, delta, hwc->last_period);
  3224. }
  3225. /*
  3226. * XXX event_limit might not quite work as expected on inherited
  3227. * events
  3228. */
  3229. event->pending_kill = POLL_IN;
  3230. if (events && atomic_dec_and_test(&event->event_limit)) {
  3231. ret = 1;
  3232. event->pending_kill = POLL_HUP;
  3233. if (nmi) {
  3234. event->pending_disable = 1;
  3235. perf_pending_queue(&event->pending,
  3236. perf_pending_event);
  3237. } else
  3238. perf_event_disable(event);
  3239. }
  3240. if (event->overflow_handler)
  3241. event->overflow_handler(event, nmi, data, regs);
  3242. else
  3243. perf_event_output(event, nmi, data, regs);
  3244. return ret;
  3245. }
  3246. int perf_event_overflow(struct perf_event *event, int nmi,
  3247. struct perf_sample_data *data,
  3248. struct pt_regs *regs)
  3249. {
  3250. return __perf_event_overflow(event, nmi, 1, data, regs);
  3251. }
  3252. /*
  3253. * Generic software event infrastructure
  3254. */
  3255. /*
  3256. * We directly increment event->count and keep a second value in
  3257. * event->hw.period_left to count intervals. This period event
  3258. * is kept in the range [-sample_period, 0] so that we can use the
  3259. * sign as trigger.
  3260. */
  3261. static u64 perf_swevent_set_period(struct perf_event *event)
  3262. {
  3263. struct hw_perf_event *hwc = &event->hw;
  3264. u64 period = hwc->last_period;
  3265. u64 nr, offset;
  3266. s64 old, val;
  3267. hwc->last_period = hwc->sample_period;
  3268. again:
  3269. old = val = atomic64_read(&hwc->period_left);
  3270. if (val < 0)
  3271. return 0;
  3272. nr = div64_u64(period + val, period);
  3273. offset = nr * period;
  3274. val -= offset;
  3275. if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
  3276. goto again;
  3277. return nr;
  3278. }
  3279. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3280. int nmi, struct perf_sample_data *data,
  3281. struct pt_regs *regs)
  3282. {
  3283. struct hw_perf_event *hwc = &event->hw;
  3284. int throttle = 0;
  3285. data->period = event->hw.last_period;
  3286. if (!overflow)
  3287. overflow = perf_swevent_set_period(event);
  3288. if (hwc->interrupts == MAX_INTERRUPTS)
  3289. return;
  3290. for (; overflow; overflow--) {
  3291. if (__perf_event_overflow(event, nmi, throttle,
  3292. data, regs)) {
  3293. /*
  3294. * We inhibit the overflow from happening when
  3295. * hwc->interrupts == MAX_INTERRUPTS.
  3296. */
  3297. break;
  3298. }
  3299. throttle = 1;
  3300. }
  3301. }
  3302. static void perf_swevent_add(struct perf_event *event, u64 nr,
  3303. int nmi, struct perf_sample_data *data,
  3304. struct pt_regs *regs)
  3305. {
  3306. struct hw_perf_event *hwc = &event->hw;
  3307. atomic64_add(nr, &event->count);
  3308. if (!regs)
  3309. return;
  3310. if (!hwc->sample_period)
  3311. return;
  3312. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3313. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3314. if (atomic64_add_negative(nr, &hwc->period_left))
  3315. return;
  3316. perf_swevent_overflow(event, 0, nmi, data, regs);
  3317. }
  3318. static int perf_exclude_event(struct perf_event *event,
  3319. struct pt_regs *regs)
  3320. {
  3321. if (regs) {
  3322. if (event->attr.exclude_user && user_mode(regs))
  3323. return 1;
  3324. if (event->attr.exclude_kernel && !user_mode(regs))
  3325. return 1;
  3326. }
  3327. return 0;
  3328. }
  3329. static int perf_swevent_match(struct perf_event *event,
  3330. enum perf_type_id type,
  3331. u32 event_id,
  3332. struct perf_sample_data *data,
  3333. struct pt_regs *regs)
  3334. {
  3335. if (event->attr.type != type)
  3336. return 0;
  3337. if (event->attr.config != event_id)
  3338. return 0;
  3339. if (perf_exclude_event(event, regs))
  3340. return 0;
  3341. return 1;
  3342. }
  3343. static inline u64 swevent_hash(u64 type, u32 event_id)
  3344. {
  3345. u64 val = event_id | (type << 32);
  3346. return hash_64(val, SWEVENT_HLIST_BITS);
  3347. }
  3348. static inline struct hlist_head *
  3349. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3350. {
  3351. u64 hash = swevent_hash(type, event_id);
  3352. return &hlist->heads[hash];
  3353. }
  3354. /* For the read side: events when they trigger */
  3355. static inline struct hlist_head *
  3356. find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
  3357. {
  3358. struct swevent_hlist *hlist;
  3359. hlist = rcu_dereference(ctx->swevent_hlist);
  3360. if (!hlist)
  3361. return NULL;
  3362. return __find_swevent_head(hlist, type, event_id);
  3363. }
  3364. /* For the event head insertion and removal in the hlist */
  3365. static inline struct hlist_head *
  3366. find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
  3367. {
  3368. struct swevent_hlist *hlist;
  3369. u32 event_id = event->attr.config;
  3370. u64 type = event->attr.type;
  3371. /*
  3372. * Event scheduling is always serialized against hlist allocation
  3373. * and release. Which makes the protected version suitable here.
  3374. * The context lock guarantees that.
  3375. */
  3376. hlist = rcu_dereference_protected(ctx->swevent_hlist,
  3377. lockdep_is_held(&event->ctx->lock));
  3378. if (!hlist)
  3379. return NULL;
  3380. return __find_swevent_head(hlist, type, event_id);
  3381. }
  3382. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3383. u64 nr, int nmi,
  3384. struct perf_sample_data *data,
  3385. struct pt_regs *regs)
  3386. {
  3387. struct perf_cpu_context *cpuctx;
  3388. struct perf_event *event;
  3389. struct hlist_node *node;
  3390. struct hlist_head *head;
  3391. cpuctx = &__get_cpu_var(perf_cpu_context);
  3392. rcu_read_lock();
  3393. head = find_swevent_head_rcu(cpuctx, type, event_id);
  3394. if (!head)
  3395. goto end;
  3396. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3397. if (perf_swevent_match(event, type, event_id, data, regs))
  3398. perf_swevent_add(event, nr, nmi, data, regs);
  3399. }
  3400. end:
  3401. rcu_read_unlock();
  3402. }
  3403. int perf_swevent_get_recursion_context(void)
  3404. {
  3405. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  3406. int rctx;
  3407. if (in_nmi())
  3408. rctx = 3;
  3409. else if (in_irq())
  3410. rctx = 2;
  3411. else if (in_softirq())
  3412. rctx = 1;
  3413. else
  3414. rctx = 0;
  3415. if (cpuctx->recursion[rctx])
  3416. return -1;
  3417. cpuctx->recursion[rctx]++;
  3418. barrier();
  3419. return rctx;
  3420. }
  3421. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3422. void perf_swevent_put_recursion_context(int rctx)
  3423. {
  3424. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  3425. barrier();
  3426. cpuctx->recursion[rctx]--;
  3427. }
  3428. EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
  3429. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3430. struct pt_regs *regs, u64 addr)
  3431. {
  3432. struct perf_sample_data data;
  3433. int rctx;
  3434. preempt_disable_notrace();
  3435. rctx = perf_swevent_get_recursion_context();
  3436. if (rctx < 0)
  3437. return;
  3438. perf_sample_data_init(&data, addr);
  3439. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3440. perf_swevent_put_recursion_context(rctx);
  3441. preempt_enable_notrace();
  3442. }
  3443. static void perf_swevent_read(struct perf_event *event)
  3444. {
  3445. }
  3446. static int perf_swevent_enable(struct perf_event *event)
  3447. {
  3448. struct hw_perf_event *hwc = &event->hw;
  3449. struct perf_cpu_context *cpuctx;
  3450. struct hlist_head *head;
  3451. cpuctx = &__get_cpu_var(perf_cpu_context);
  3452. if (hwc->sample_period) {
  3453. hwc->last_period = hwc->sample_period;
  3454. perf_swevent_set_period(event);
  3455. }
  3456. head = find_swevent_head(cpuctx, event);
  3457. if (WARN_ON_ONCE(!head))
  3458. return -EINVAL;
  3459. hlist_add_head_rcu(&event->hlist_entry, head);
  3460. return 0;
  3461. }
  3462. static void perf_swevent_disable(struct perf_event *event)
  3463. {
  3464. hlist_del_rcu(&event->hlist_entry);
  3465. }
  3466. static void perf_swevent_void(struct perf_event *event)
  3467. {
  3468. }
  3469. static int perf_swevent_int(struct perf_event *event)
  3470. {
  3471. return 0;
  3472. }
  3473. static const struct pmu perf_ops_generic = {
  3474. .enable = perf_swevent_enable,
  3475. .disable = perf_swevent_disable,
  3476. .start = perf_swevent_int,
  3477. .stop = perf_swevent_void,
  3478. .read = perf_swevent_read,
  3479. .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
  3480. };
  3481. /*
  3482. * hrtimer based swevent callback
  3483. */
  3484. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  3485. {
  3486. enum hrtimer_restart ret = HRTIMER_RESTART;
  3487. struct perf_sample_data data;
  3488. struct pt_regs *regs;
  3489. struct perf_event *event;
  3490. u64 period;
  3491. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  3492. event->pmu->read(event);
  3493. perf_sample_data_init(&data, 0);
  3494. data.period = event->hw.last_period;
  3495. regs = get_irq_regs();
  3496. if (regs && !perf_exclude_event(event, regs)) {
  3497. if (!(event->attr.exclude_idle && current->pid == 0))
  3498. if (perf_event_overflow(event, 0, &data, regs))
  3499. ret = HRTIMER_NORESTART;
  3500. }
  3501. period = max_t(u64, 10000, event->hw.sample_period);
  3502. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  3503. return ret;
  3504. }
  3505. static void perf_swevent_start_hrtimer(struct perf_event *event)
  3506. {
  3507. struct hw_perf_event *hwc = &event->hw;
  3508. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3509. hwc->hrtimer.function = perf_swevent_hrtimer;
  3510. if (hwc->sample_period) {
  3511. u64 period;
  3512. if (hwc->remaining) {
  3513. if (hwc->remaining < 0)
  3514. period = 10000;
  3515. else
  3516. period = hwc->remaining;
  3517. hwc->remaining = 0;
  3518. } else {
  3519. period = max_t(u64, 10000, hwc->sample_period);
  3520. }
  3521. __hrtimer_start_range_ns(&hwc->hrtimer,
  3522. ns_to_ktime(period), 0,
  3523. HRTIMER_MODE_REL, 0);
  3524. }
  3525. }
  3526. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  3527. {
  3528. struct hw_perf_event *hwc = &event->hw;
  3529. if (hwc->sample_period) {
  3530. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  3531. hwc->remaining = ktime_to_ns(remaining);
  3532. hrtimer_cancel(&hwc->hrtimer);
  3533. }
  3534. }
  3535. /*
  3536. * Software event: cpu wall time clock
  3537. */
  3538. static void cpu_clock_perf_event_update(struct perf_event *event)
  3539. {
  3540. int cpu = raw_smp_processor_id();
  3541. s64 prev;
  3542. u64 now;
  3543. now = cpu_clock(cpu);
  3544. prev = atomic64_xchg(&event->hw.prev_count, now);
  3545. atomic64_add(now - prev, &event->count);
  3546. }
  3547. static int cpu_clock_perf_event_enable(struct perf_event *event)
  3548. {
  3549. struct hw_perf_event *hwc = &event->hw;
  3550. int cpu = raw_smp_processor_id();
  3551. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  3552. perf_swevent_start_hrtimer(event);
  3553. return 0;
  3554. }
  3555. static void cpu_clock_perf_event_disable(struct perf_event *event)
  3556. {
  3557. perf_swevent_cancel_hrtimer(event);
  3558. cpu_clock_perf_event_update(event);
  3559. }
  3560. static void cpu_clock_perf_event_read(struct perf_event *event)
  3561. {
  3562. cpu_clock_perf_event_update(event);
  3563. }
  3564. static const struct pmu perf_ops_cpu_clock = {
  3565. .enable = cpu_clock_perf_event_enable,
  3566. .disable = cpu_clock_perf_event_disable,
  3567. .read = cpu_clock_perf_event_read,
  3568. };
  3569. /*
  3570. * Software event: task time clock
  3571. */
  3572. static void task_clock_perf_event_update(struct perf_event *event, u64 now)
  3573. {
  3574. u64 prev;
  3575. s64 delta;
  3576. prev = atomic64_xchg(&event->hw.prev_count, now);
  3577. delta = now - prev;
  3578. atomic64_add(delta, &event->count);
  3579. }
  3580. static int task_clock_perf_event_enable(struct perf_event *event)
  3581. {
  3582. struct hw_perf_event *hwc = &event->hw;
  3583. u64 now;
  3584. now = event->ctx->time;
  3585. atomic64_set(&hwc->prev_count, now);
  3586. perf_swevent_start_hrtimer(event);
  3587. return 0;
  3588. }
  3589. static void task_clock_perf_event_disable(struct perf_event *event)
  3590. {
  3591. perf_swevent_cancel_hrtimer(event);
  3592. task_clock_perf_event_update(event, event->ctx->time);
  3593. }
  3594. static void task_clock_perf_event_read(struct perf_event *event)
  3595. {
  3596. u64 time;
  3597. if (!in_nmi()) {
  3598. update_context_time(event->ctx);
  3599. time = event->ctx->time;
  3600. } else {
  3601. u64 now = perf_clock();
  3602. u64 delta = now - event->ctx->timestamp;
  3603. time = event->ctx->time + delta;
  3604. }
  3605. task_clock_perf_event_update(event, time);
  3606. }
  3607. static const struct pmu perf_ops_task_clock = {
  3608. .enable = task_clock_perf_event_enable,
  3609. .disable = task_clock_perf_event_disable,
  3610. .read = task_clock_perf_event_read,
  3611. };
  3612. /* Deref the hlist from the update side */
  3613. static inline struct swevent_hlist *
  3614. swevent_hlist_deref(struct perf_cpu_context *cpuctx)
  3615. {
  3616. return rcu_dereference_protected(cpuctx->swevent_hlist,
  3617. lockdep_is_held(&cpuctx->hlist_mutex));
  3618. }
  3619. static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
  3620. {
  3621. struct swevent_hlist *hlist;
  3622. hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
  3623. kfree(hlist);
  3624. }
  3625. static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
  3626. {
  3627. struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
  3628. if (!hlist)
  3629. return;
  3630. rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
  3631. call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
  3632. }
  3633. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  3634. {
  3635. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  3636. mutex_lock(&cpuctx->hlist_mutex);
  3637. if (!--cpuctx->hlist_refcount)
  3638. swevent_hlist_release(cpuctx);
  3639. mutex_unlock(&cpuctx->hlist_mutex);
  3640. }
  3641. static void swevent_hlist_put(struct perf_event *event)
  3642. {
  3643. int cpu;
  3644. if (event->cpu != -1) {
  3645. swevent_hlist_put_cpu(event, event->cpu);
  3646. return;
  3647. }
  3648. for_each_possible_cpu(cpu)
  3649. swevent_hlist_put_cpu(event, cpu);
  3650. }
  3651. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  3652. {
  3653. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  3654. int err = 0;
  3655. mutex_lock(&cpuctx->hlist_mutex);
  3656. if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
  3657. struct swevent_hlist *hlist;
  3658. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  3659. if (!hlist) {
  3660. err = -ENOMEM;
  3661. goto exit;
  3662. }
  3663. rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
  3664. }
  3665. cpuctx->hlist_refcount++;
  3666. exit:
  3667. mutex_unlock(&cpuctx->hlist_mutex);
  3668. return err;
  3669. }
  3670. static int swevent_hlist_get(struct perf_event *event)
  3671. {
  3672. int err;
  3673. int cpu, failed_cpu;
  3674. if (event->cpu != -1)
  3675. return swevent_hlist_get_cpu(event, event->cpu);
  3676. get_online_cpus();
  3677. for_each_possible_cpu(cpu) {
  3678. err = swevent_hlist_get_cpu(event, cpu);
  3679. if (err) {
  3680. failed_cpu = cpu;
  3681. goto fail;
  3682. }
  3683. }
  3684. put_online_cpus();
  3685. return 0;
  3686. fail:
  3687. for_each_possible_cpu(cpu) {
  3688. if (cpu == failed_cpu)
  3689. break;
  3690. swevent_hlist_put_cpu(event, cpu);
  3691. }
  3692. put_online_cpus();
  3693. return err;
  3694. }
  3695. #ifdef CONFIG_EVENT_TRACING
  3696. static const struct pmu perf_ops_tracepoint = {
  3697. .enable = perf_trace_enable,
  3698. .disable = perf_trace_disable,
  3699. .start = perf_swevent_int,
  3700. .stop = perf_swevent_void,
  3701. .read = perf_swevent_read,
  3702. .unthrottle = perf_swevent_void,
  3703. };
  3704. static int perf_tp_filter_match(struct perf_event *event,
  3705. struct perf_sample_data *data)
  3706. {
  3707. void *record = data->raw->data;
  3708. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3709. return 1;
  3710. return 0;
  3711. }
  3712. static int perf_tp_event_match(struct perf_event *event,
  3713. struct perf_sample_data *data,
  3714. struct pt_regs *regs)
  3715. {
  3716. /*
  3717. * All tracepoints are from kernel-space.
  3718. */
  3719. if (event->attr.exclude_kernel)
  3720. return 0;
  3721. if (!perf_tp_filter_match(event, data))
  3722. return 0;
  3723. return 1;
  3724. }
  3725. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  3726. struct pt_regs *regs, struct hlist_head *head)
  3727. {
  3728. struct perf_sample_data data;
  3729. struct perf_event *event;
  3730. struct hlist_node *node;
  3731. struct perf_raw_record raw = {
  3732. .size = entry_size,
  3733. .data = record,
  3734. };
  3735. perf_sample_data_init(&data, addr);
  3736. data.raw = &raw;
  3737. rcu_read_lock();
  3738. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3739. if (perf_tp_event_match(event, &data, regs))
  3740. perf_swevent_add(event, count, 1, &data, regs);
  3741. }
  3742. rcu_read_unlock();
  3743. }
  3744. EXPORT_SYMBOL_GPL(perf_tp_event);
  3745. static void tp_perf_event_destroy(struct perf_event *event)
  3746. {
  3747. perf_trace_destroy(event);
  3748. }
  3749. static const struct pmu *tp_perf_event_init(struct perf_event *event)
  3750. {
  3751. int err;
  3752. /*
  3753. * Raw tracepoint data is a severe data leak, only allow root to
  3754. * have these.
  3755. */
  3756. if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
  3757. perf_paranoid_tracepoint_raw() &&
  3758. !capable(CAP_SYS_ADMIN))
  3759. return ERR_PTR(-EPERM);
  3760. err = perf_trace_init(event);
  3761. if (err)
  3762. return NULL;
  3763. event->destroy = tp_perf_event_destroy;
  3764. return &perf_ops_tracepoint;
  3765. }
  3766. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3767. {
  3768. char *filter_str;
  3769. int ret;
  3770. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3771. return -EINVAL;
  3772. filter_str = strndup_user(arg, PAGE_SIZE);
  3773. if (IS_ERR(filter_str))
  3774. return PTR_ERR(filter_str);
  3775. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  3776. kfree(filter_str);
  3777. return ret;
  3778. }
  3779. static void perf_event_free_filter(struct perf_event *event)
  3780. {
  3781. ftrace_profile_free_filter(event);
  3782. }
  3783. #else
  3784. static const struct pmu *tp_perf_event_init(struct perf_event *event)
  3785. {
  3786. return NULL;
  3787. }
  3788. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3789. {
  3790. return -ENOENT;
  3791. }
  3792. static void perf_event_free_filter(struct perf_event *event)
  3793. {
  3794. }
  3795. #endif /* CONFIG_EVENT_TRACING */
  3796. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  3797. static void bp_perf_event_destroy(struct perf_event *event)
  3798. {
  3799. release_bp_slot(event);
  3800. }
  3801. static const struct pmu *bp_perf_event_init(struct perf_event *bp)
  3802. {
  3803. int err;
  3804. err = register_perf_hw_breakpoint(bp);
  3805. if (err)
  3806. return ERR_PTR(err);
  3807. bp->destroy = bp_perf_event_destroy;
  3808. return &perf_ops_bp;
  3809. }
  3810. void perf_bp_event(struct perf_event *bp, void *data)
  3811. {
  3812. struct perf_sample_data sample;
  3813. struct pt_regs *regs = data;
  3814. perf_sample_data_init(&sample, bp->attr.bp_addr);
  3815. if (!perf_exclude_event(bp, regs))
  3816. perf_swevent_add(bp, 1, 1, &sample, regs);
  3817. }
  3818. #else
  3819. static const struct pmu *bp_perf_event_init(struct perf_event *bp)
  3820. {
  3821. return NULL;
  3822. }
  3823. void perf_bp_event(struct perf_event *bp, void *regs)
  3824. {
  3825. }
  3826. #endif
  3827. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3828. static void sw_perf_event_destroy(struct perf_event *event)
  3829. {
  3830. u64 event_id = event->attr.config;
  3831. WARN_ON(event->parent);
  3832. atomic_dec(&perf_swevent_enabled[event_id]);
  3833. swevent_hlist_put(event);
  3834. }
  3835. static const struct pmu *sw_perf_event_init(struct perf_event *event)
  3836. {
  3837. const struct pmu *pmu = NULL;
  3838. u64 event_id = event->attr.config;
  3839. /*
  3840. * Software events (currently) can't in general distinguish
  3841. * between user, kernel and hypervisor events.
  3842. * However, context switches and cpu migrations are considered
  3843. * to be kernel events, and page faults are never hypervisor
  3844. * events.
  3845. */
  3846. switch (event_id) {
  3847. case PERF_COUNT_SW_CPU_CLOCK:
  3848. pmu = &perf_ops_cpu_clock;
  3849. break;
  3850. case PERF_COUNT_SW_TASK_CLOCK:
  3851. /*
  3852. * If the user instantiates this as a per-cpu event,
  3853. * use the cpu_clock event instead.
  3854. */
  3855. if (event->ctx->task)
  3856. pmu = &perf_ops_task_clock;
  3857. else
  3858. pmu = &perf_ops_cpu_clock;
  3859. break;
  3860. case PERF_COUNT_SW_PAGE_FAULTS:
  3861. case PERF_COUNT_SW_PAGE_FAULTS_MIN:
  3862. case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
  3863. case PERF_COUNT_SW_CONTEXT_SWITCHES:
  3864. case PERF_COUNT_SW_CPU_MIGRATIONS:
  3865. case PERF_COUNT_SW_ALIGNMENT_FAULTS:
  3866. case PERF_COUNT_SW_EMULATION_FAULTS:
  3867. if (!event->parent) {
  3868. int err;
  3869. err = swevent_hlist_get(event);
  3870. if (err)
  3871. return ERR_PTR(err);
  3872. atomic_inc(&perf_swevent_enabled[event_id]);
  3873. event->destroy = sw_perf_event_destroy;
  3874. }
  3875. pmu = &perf_ops_generic;
  3876. break;
  3877. }
  3878. return pmu;
  3879. }
  3880. /*
  3881. * Allocate and initialize a event structure
  3882. */
  3883. static struct perf_event *
  3884. perf_event_alloc(struct perf_event_attr *attr,
  3885. int cpu,
  3886. struct perf_event_context *ctx,
  3887. struct perf_event *group_leader,
  3888. struct perf_event *parent_event,
  3889. perf_overflow_handler_t overflow_handler,
  3890. gfp_t gfpflags)
  3891. {
  3892. const struct pmu *pmu;
  3893. struct perf_event *event;
  3894. struct hw_perf_event *hwc;
  3895. long err;
  3896. event = kzalloc(sizeof(*event), gfpflags);
  3897. if (!event)
  3898. return ERR_PTR(-ENOMEM);
  3899. /*
  3900. * Single events are their own group leaders, with an
  3901. * empty sibling list:
  3902. */
  3903. if (!group_leader)
  3904. group_leader = event;
  3905. mutex_init(&event->child_mutex);
  3906. INIT_LIST_HEAD(&event->child_list);
  3907. INIT_LIST_HEAD(&event->group_entry);
  3908. INIT_LIST_HEAD(&event->event_entry);
  3909. INIT_LIST_HEAD(&event->sibling_list);
  3910. init_waitqueue_head(&event->waitq);
  3911. mutex_init(&event->mmap_mutex);
  3912. event->cpu = cpu;
  3913. event->attr = *attr;
  3914. event->group_leader = group_leader;
  3915. event->pmu = NULL;
  3916. event->ctx = ctx;
  3917. event->oncpu = -1;
  3918. event->parent = parent_event;
  3919. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  3920. event->id = atomic64_inc_return(&perf_event_id);
  3921. event->state = PERF_EVENT_STATE_INACTIVE;
  3922. if (!overflow_handler && parent_event)
  3923. overflow_handler = parent_event->overflow_handler;
  3924. event->overflow_handler = overflow_handler;
  3925. if (attr->disabled)
  3926. event->state = PERF_EVENT_STATE_OFF;
  3927. pmu = NULL;
  3928. hwc = &event->hw;
  3929. hwc->sample_period = attr->sample_period;
  3930. if (attr->freq && attr->sample_freq)
  3931. hwc->sample_period = 1;
  3932. hwc->last_period = hwc->sample_period;
  3933. atomic64_set(&hwc->period_left, hwc->sample_period);
  3934. /*
  3935. * we currently do not support PERF_FORMAT_GROUP on inherited events
  3936. */
  3937. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  3938. goto done;
  3939. switch (attr->type) {
  3940. case PERF_TYPE_RAW:
  3941. case PERF_TYPE_HARDWARE:
  3942. case PERF_TYPE_HW_CACHE:
  3943. pmu = hw_perf_event_init(event);
  3944. break;
  3945. case PERF_TYPE_SOFTWARE:
  3946. pmu = sw_perf_event_init(event);
  3947. break;
  3948. case PERF_TYPE_TRACEPOINT:
  3949. pmu = tp_perf_event_init(event);
  3950. break;
  3951. case PERF_TYPE_BREAKPOINT:
  3952. pmu = bp_perf_event_init(event);
  3953. break;
  3954. default:
  3955. break;
  3956. }
  3957. done:
  3958. err = 0;
  3959. if (!pmu)
  3960. err = -EINVAL;
  3961. else if (IS_ERR(pmu))
  3962. err = PTR_ERR(pmu);
  3963. if (err) {
  3964. if (event->ns)
  3965. put_pid_ns(event->ns);
  3966. kfree(event);
  3967. return ERR_PTR(err);
  3968. }
  3969. event->pmu = pmu;
  3970. if (!event->parent) {
  3971. atomic_inc(&nr_events);
  3972. if (event->attr.mmap)
  3973. atomic_inc(&nr_mmap_events);
  3974. if (event->attr.comm)
  3975. atomic_inc(&nr_comm_events);
  3976. if (event->attr.task)
  3977. atomic_inc(&nr_task_events);
  3978. }
  3979. return event;
  3980. }
  3981. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  3982. struct perf_event_attr *attr)
  3983. {
  3984. u32 size;
  3985. int ret;
  3986. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  3987. return -EFAULT;
  3988. /*
  3989. * zero the full structure, so that a short copy will be nice.
  3990. */
  3991. memset(attr, 0, sizeof(*attr));
  3992. ret = get_user(size, &uattr->size);
  3993. if (ret)
  3994. return ret;
  3995. if (size > PAGE_SIZE) /* silly large */
  3996. goto err_size;
  3997. if (!size) /* abi compat */
  3998. size = PERF_ATTR_SIZE_VER0;
  3999. if (size < PERF_ATTR_SIZE_VER0)
  4000. goto err_size;
  4001. /*
  4002. * If we're handed a bigger struct than we know of,
  4003. * ensure all the unknown bits are 0 - i.e. new
  4004. * user-space does not rely on any kernel feature
  4005. * extensions we dont know about yet.
  4006. */
  4007. if (size > sizeof(*attr)) {
  4008. unsigned char __user *addr;
  4009. unsigned char __user *end;
  4010. unsigned char val;
  4011. addr = (void __user *)uattr + sizeof(*attr);
  4012. end = (void __user *)uattr + size;
  4013. for (; addr < end; addr++) {
  4014. ret = get_user(val, addr);
  4015. if (ret)
  4016. return ret;
  4017. if (val)
  4018. goto err_size;
  4019. }
  4020. size = sizeof(*attr);
  4021. }
  4022. ret = copy_from_user(attr, uattr, size);
  4023. if (ret)
  4024. return -EFAULT;
  4025. /*
  4026. * If the type exists, the corresponding creation will verify
  4027. * the attr->config.
  4028. */
  4029. if (attr->type >= PERF_TYPE_MAX)
  4030. return -EINVAL;
  4031. if (attr->__reserved_1)
  4032. return -EINVAL;
  4033. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4034. return -EINVAL;
  4035. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4036. return -EINVAL;
  4037. out:
  4038. return ret;
  4039. err_size:
  4040. put_user(sizeof(*attr), &uattr->size);
  4041. ret = -E2BIG;
  4042. goto out;
  4043. }
  4044. static int
  4045. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4046. {
  4047. struct perf_mmap_data *data = NULL, *old_data = NULL;
  4048. int ret = -EINVAL;
  4049. if (!output_event)
  4050. goto set;
  4051. /* don't allow circular references */
  4052. if (event == output_event)
  4053. goto out;
  4054. /*
  4055. * Don't allow cross-cpu buffers
  4056. */
  4057. if (output_event->cpu != event->cpu)
  4058. goto out;
  4059. /*
  4060. * If its not a per-cpu buffer, it must be the same task.
  4061. */
  4062. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4063. goto out;
  4064. set:
  4065. mutex_lock(&event->mmap_mutex);
  4066. /* Can't redirect output if we've got an active mmap() */
  4067. if (atomic_read(&event->mmap_count))
  4068. goto unlock;
  4069. if (output_event) {
  4070. /* get the buffer we want to redirect to */
  4071. data = perf_mmap_data_get(output_event);
  4072. if (!data)
  4073. goto unlock;
  4074. }
  4075. old_data = event->data;
  4076. rcu_assign_pointer(event->data, data);
  4077. ret = 0;
  4078. unlock:
  4079. mutex_unlock(&event->mmap_mutex);
  4080. if (old_data)
  4081. perf_mmap_data_put(old_data);
  4082. out:
  4083. return ret;
  4084. }
  4085. /**
  4086. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4087. *
  4088. * @attr_uptr: event_id type attributes for monitoring/sampling
  4089. * @pid: target pid
  4090. * @cpu: target cpu
  4091. * @group_fd: group leader event fd
  4092. */
  4093. SYSCALL_DEFINE5(perf_event_open,
  4094. struct perf_event_attr __user *, attr_uptr,
  4095. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4096. {
  4097. struct perf_event *event, *group_leader = NULL, *output_event = NULL;
  4098. struct perf_event_attr attr;
  4099. struct perf_event_context *ctx;
  4100. struct file *event_file = NULL;
  4101. struct file *group_file = NULL;
  4102. int event_fd;
  4103. int fput_needed = 0;
  4104. int err;
  4105. /* for future expandability... */
  4106. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  4107. return -EINVAL;
  4108. err = perf_copy_attr(attr_uptr, &attr);
  4109. if (err)
  4110. return err;
  4111. if (!attr.exclude_kernel) {
  4112. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4113. return -EACCES;
  4114. }
  4115. if (attr.freq) {
  4116. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4117. return -EINVAL;
  4118. }
  4119. event_fd = get_unused_fd_flags(O_RDWR);
  4120. if (event_fd < 0)
  4121. return event_fd;
  4122. /*
  4123. * Get the target context (task or percpu):
  4124. */
  4125. ctx = find_get_context(pid, cpu);
  4126. if (IS_ERR(ctx)) {
  4127. err = PTR_ERR(ctx);
  4128. goto err_fd;
  4129. }
  4130. if (group_fd != -1) {
  4131. group_leader = perf_fget_light(group_fd, &fput_needed);
  4132. if (IS_ERR(group_leader)) {
  4133. err = PTR_ERR(group_leader);
  4134. goto err_put_context;
  4135. }
  4136. group_file = group_leader->filp;
  4137. if (flags & PERF_FLAG_FD_OUTPUT)
  4138. output_event = group_leader;
  4139. if (flags & PERF_FLAG_FD_NO_GROUP)
  4140. group_leader = NULL;
  4141. }
  4142. /*
  4143. * Look up the group leader (we will attach this event to it):
  4144. */
  4145. if (group_leader) {
  4146. err = -EINVAL;
  4147. /*
  4148. * Do not allow a recursive hierarchy (this new sibling
  4149. * becoming part of another group-sibling):
  4150. */
  4151. if (group_leader->group_leader != group_leader)
  4152. goto err_put_context;
  4153. /*
  4154. * Do not allow to attach to a group in a different
  4155. * task or CPU context:
  4156. */
  4157. if (group_leader->ctx != ctx)
  4158. goto err_put_context;
  4159. /*
  4160. * Only a group leader can be exclusive or pinned
  4161. */
  4162. if (attr.exclusive || attr.pinned)
  4163. goto err_put_context;
  4164. }
  4165. event = perf_event_alloc(&attr, cpu, ctx, group_leader,
  4166. NULL, NULL, GFP_KERNEL);
  4167. if (IS_ERR(event)) {
  4168. err = PTR_ERR(event);
  4169. goto err_put_context;
  4170. }
  4171. if (output_event) {
  4172. err = perf_event_set_output(event, output_event);
  4173. if (err)
  4174. goto err_free_put_context;
  4175. }
  4176. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  4177. if (IS_ERR(event_file)) {
  4178. err = PTR_ERR(event_file);
  4179. goto err_free_put_context;
  4180. }
  4181. event->filp = event_file;
  4182. WARN_ON_ONCE(ctx->parent_ctx);
  4183. mutex_lock(&ctx->mutex);
  4184. perf_install_in_context(ctx, event, cpu);
  4185. ++ctx->generation;
  4186. mutex_unlock(&ctx->mutex);
  4187. event->owner = current;
  4188. get_task_struct(current);
  4189. mutex_lock(&current->perf_event_mutex);
  4190. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4191. mutex_unlock(&current->perf_event_mutex);
  4192. /*
  4193. * Drop the reference on the group_event after placing the
  4194. * new event on the sibling_list. This ensures destruction
  4195. * of the group leader will find the pointer to itself in
  4196. * perf_group_detach().
  4197. */
  4198. fput_light(group_file, fput_needed);
  4199. fd_install(event_fd, event_file);
  4200. return event_fd;
  4201. err_free_put_context:
  4202. free_event(event);
  4203. err_put_context:
  4204. fput_light(group_file, fput_needed);
  4205. put_ctx(ctx);
  4206. err_fd:
  4207. put_unused_fd(event_fd);
  4208. return err;
  4209. }
  4210. /**
  4211. * perf_event_create_kernel_counter
  4212. *
  4213. * @attr: attributes of the counter to create
  4214. * @cpu: cpu in which the counter is bound
  4215. * @pid: task to profile
  4216. */
  4217. struct perf_event *
  4218. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  4219. pid_t pid,
  4220. perf_overflow_handler_t overflow_handler)
  4221. {
  4222. struct perf_event *event;
  4223. struct perf_event_context *ctx;
  4224. int err;
  4225. /*
  4226. * Get the target context (task or percpu):
  4227. */
  4228. ctx = find_get_context(pid, cpu);
  4229. if (IS_ERR(ctx)) {
  4230. err = PTR_ERR(ctx);
  4231. goto err_exit;
  4232. }
  4233. event = perf_event_alloc(attr, cpu, ctx, NULL,
  4234. NULL, overflow_handler, GFP_KERNEL);
  4235. if (IS_ERR(event)) {
  4236. err = PTR_ERR(event);
  4237. goto err_put_context;
  4238. }
  4239. event->filp = NULL;
  4240. WARN_ON_ONCE(ctx->parent_ctx);
  4241. mutex_lock(&ctx->mutex);
  4242. perf_install_in_context(ctx, event, cpu);
  4243. ++ctx->generation;
  4244. mutex_unlock(&ctx->mutex);
  4245. event->owner = current;
  4246. get_task_struct(current);
  4247. mutex_lock(&current->perf_event_mutex);
  4248. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4249. mutex_unlock(&current->perf_event_mutex);
  4250. return event;
  4251. err_put_context:
  4252. put_ctx(ctx);
  4253. err_exit:
  4254. return ERR_PTR(err);
  4255. }
  4256. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  4257. /*
  4258. * inherit a event from parent task to child task:
  4259. */
  4260. static struct perf_event *
  4261. inherit_event(struct perf_event *parent_event,
  4262. struct task_struct *parent,
  4263. struct perf_event_context *parent_ctx,
  4264. struct task_struct *child,
  4265. struct perf_event *group_leader,
  4266. struct perf_event_context *child_ctx)
  4267. {
  4268. struct perf_event *child_event;
  4269. /*
  4270. * Instead of creating recursive hierarchies of events,
  4271. * we link inherited events back to the original parent,
  4272. * which has a filp for sure, which we use as the reference
  4273. * count:
  4274. */
  4275. if (parent_event->parent)
  4276. parent_event = parent_event->parent;
  4277. child_event = perf_event_alloc(&parent_event->attr,
  4278. parent_event->cpu, child_ctx,
  4279. group_leader, parent_event,
  4280. NULL, GFP_KERNEL);
  4281. if (IS_ERR(child_event))
  4282. return child_event;
  4283. get_ctx(child_ctx);
  4284. /*
  4285. * Make the child state follow the state of the parent event,
  4286. * not its attr.disabled bit. We hold the parent's mutex,
  4287. * so we won't race with perf_event_{en, dis}able_family.
  4288. */
  4289. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  4290. child_event->state = PERF_EVENT_STATE_INACTIVE;
  4291. else
  4292. child_event->state = PERF_EVENT_STATE_OFF;
  4293. if (parent_event->attr.freq) {
  4294. u64 sample_period = parent_event->hw.sample_period;
  4295. struct hw_perf_event *hwc = &child_event->hw;
  4296. hwc->sample_period = sample_period;
  4297. hwc->last_period = sample_period;
  4298. atomic64_set(&hwc->period_left, sample_period);
  4299. }
  4300. child_event->overflow_handler = parent_event->overflow_handler;
  4301. /*
  4302. * Link it up in the child's context:
  4303. */
  4304. add_event_to_ctx(child_event, child_ctx);
  4305. /*
  4306. * Get a reference to the parent filp - we will fput it
  4307. * when the child event exits. This is safe to do because
  4308. * we are in the parent and we know that the filp still
  4309. * exists and has a nonzero count:
  4310. */
  4311. atomic_long_inc(&parent_event->filp->f_count);
  4312. /*
  4313. * Link this into the parent event's child list
  4314. */
  4315. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4316. mutex_lock(&parent_event->child_mutex);
  4317. list_add_tail(&child_event->child_list, &parent_event->child_list);
  4318. mutex_unlock(&parent_event->child_mutex);
  4319. return child_event;
  4320. }
  4321. static int inherit_group(struct perf_event *parent_event,
  4322. struct task_struct *parent,
  4323. struct perf_event_context *parent_ctx,
  4324. struct task_struct *child,
  4325. struct perf_event_context *child_ctx)
  4326. {
  4327. struct perf_event *leader;
  4328. struct perf_event *sub;
  4329. struct perf_event *child_ctr;
  4330. leader = inherit_event(parent_event, parent, parent_ctx,
  4331. child, NULL, child_ctx);
  4332. if (IS_ERR(leader))
  4333. return PTR_ERR(leader);
  4334. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  4335. child_ctr = inherit_event(sub, parent, parent_ctx,
  4336. child, leader, child_ctx);
  4337. if (IS_ERR(child_ctr))
  4338. return PTR_ERR(child_ctr);
  4339. }
  4340. return 0;
  4341. }
  4342. static void sync_child_event(struct perf_event *child_event,
  4343. struct task_struct *child)
  4344. {
  4345. struct perf_event *parent_event = child_event->parent;
  4346. u64 child_val;
  4347. if (child_event->attr.inherit_stat)
  4348. perf_event_read_event(child_event, child);
  4349. child_val = atomic64_read(&child_event->count);
  4350. /*
  4351. * Add back the child's count to the parent's count:
  4352. */
  4353. atomic64_add(child_val, &parent_event->count);
  4354. atomic64_add(child_event->total_time_enabled,
  4355. &parent_event->child_total_time_enabled);
  4356. atomic64_add(child_event->total_time_running,
  4357. &parent_event->child_total_time_running);
  4358. /*
  4359. * Remove this event from the parent's list
  4360. */
  4361. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4362. mutex_lock(&parent_event->child_mutex);
  4363. list_del_init(&child_event->child_list);
  4364. mutex_unlock(&parent_event->child_mutex);
  4365. /*
  4366. * Release the parent event, if this was the last
  4367. * reference to it.
  4368. */
  4369. fput(parent_event->filp);
  4370. }
  4371. static void
  4372. __perf_event_exit_task(struct perf_event *child_event,
  4373. struct perf_event_context *child_ctx,
  4374. struct task_struct *child)
  4375. {
  4376. struct perf_event *parent_event;
  4377. perf_event_remove_from_context(child_event);
  4378. parent_event = child_event->parent;
  4379. /*
  4380. * It can happen that parent exits first, and has events
  4381. * that are still around due to the child reference. These
  4382. * events need to be zapped - but otherwise linger.
  4383. */
  4384. if (parent_event) {
  4385. sync_child_event(child_event, child);
  4386. free_event(child_event);
  4387. }
  4388. }
  4389. /*
  4390. * When a child task exits, feed back event values to parent events.
  4391. */
  4392. void perf_event_exit_task(struct task_struct *child)
  4393. {
  4394. struct perf_event *child_event, *tmp;
  4395. struct perf_event_context *child_ctx;
  4396. unsigned long flags;
  4397. if (likely(!child->perf_event_ctxp)) {
  4398. perf_event_task(child, NULL, 0);
  4399. return;
  4400. }
  4401. local_irq_save(flags);
  4402. /*
  4403. * We can't reschedule here because interrupts are disabled,
  4404. * and either child is current or it is a task that can't be
  4405. * scheduled, so we are now safe from rescheduling changing
  4406. * our context.
  4407. */
  4408. child_ctx = child->perf_event_ctxp;
  4409. __perf_event_task_sched_out(child_ctx);
  4410. /*
  4411. * Take the context lock here so that if find_get_context is
  4412. * reading child->perf_event_ctxp, we wait until it has
  4413. * incremented the context's refcount before we do put_ctx below.
  4414. */
  4415. raw_spin_lock(&child_ctx->lock);
  4416. child->perf_event_ctxp = NULL;
  4417. /*
  4418. * If this context is a clone; unclone it so it can't get
  4419. * swapped to another process while we're removing all
  4420. * the events from it.
  4421. */
  4422. unclone_ctx(child_ctx);
  4423. update_context_time(child_ctx);
  4424. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4425. /*
  4426. * Report the task dead after unscheduling the events so that we
  4427. * won't get any samples after PERF_RECORD_EXIT. We can however still
  4428. * get a few PERF_RECORD_READ events.
  4429. */
  4430. perf_event_task(child, child_ctx, 0);
  4431. /*
  4432. * We can recurse on the same lock type through:
  4433. *
  4434. * __perf_event_exit_task()
  4435. * sync_child_event()
  4436. * fput(parent_event->filp)
  4437. * perf_release()
  4438. * mutex_lock(&ctx->mutex)
  4439. *
  4440. * But since its the parent context it won't be the same instance.
  4441. */
  4442. mutex_lock(&child_ctx->mutex);
  4443. again:
  4444. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  4445. group_entry)
  4446. __perf_event_exit_task(child_event, child_ctx, child);
  4447. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  4448. group_entry)
  4449. __perf_event_exit_task(child_event, child_ctx, child);
  4450. /*
  4451. * If the last event was a group event, it will have appended all
  4452. * its siblings to the list, but we obtained 'tmp' before that which
  4453. * will still point to the list head terminating the iteration.
  4454. */
  4455. if (!list_empty(&child_ctx->pinned_groups) ||
  4456. !list_empty(&child_ctx->flexible_groups))
  4457. goto again;
  4458. mutex_unlock(&child_ctx->mutex);
  4459. put_ctx(child_ctx);
  4460. }
  4461. static void perf_free_event(struct perf_event *event,
  4462. struct perf_event_context *ctx)
  4463. {
  4464. struct perf_event *parent = event->parent;
  4465. if (WARN_ON_ONCE(!parent))
  4466. return;
  4467. mutex_lock(&parent->child_mutex);
  4468. list_del_init(&event->child_list);
  4469. mutex_unlock(&parent->child_mutex);
  4470. fput(parent->filp);
  4471. perf_group_detach(event);
  4472. list_del_event(event, ctx);
  4473. free_event(event);
  4474. }
  4475. /*
  4476. * free an unexposed, unused context as created by inheritance by
  4477. * init_task below, used by fork() in case of fail.
  4478. */
  4479. void perf_event_free_task(struct task_struct *task)
  4480. {
  4481. struct perf_event_context *ctx = task->perf_event_ctxp;
  4482. struct perf_event *event, *tmp;
  4483. if (!ctx)
  4484. return;
  4485. mutex_lock(&ctx->mutex);
  4486. again:
  4487. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  4488. perf_free_event(event, ctx);
  4489. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  4490. group_entry)
  4491. perf_free_event(event, ctx);
  4492. if (!list_empty(&ctx->pinned_groups) ||
  4493. !list_empty(&ctx->flexible_groups))
  4494. goto again;
  4495. mutex_unlock(&ctx->mutex);
  4496. put_ctx(ctx);
  4497. }
  4498. static int
  4499. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  4500. struct perf_event_context *parent_ctx,
  4501. struct task_struct *child,
  4502. int *inherited_all)
  4503. {
  4504. int ret;
  4505. struct perf_event_context *child_ctx = child->perf_event_ctxp;
  4506. if (!event->attr.inherit) {
  4507. *inherited_all = 0;
  4508. return 0;
  4509. }
  4510. if (!child_ctx) {
  4511. /*
  4512. * This is executed from the parent task context, so
  4513. * inherit events that have been marked for cloning.
  4514. * First allocate and initialize a context for the
  4515. * child.
  4516. */
  4517. child_ctx = kzalloc(sizeof(struct perf_event_context),
  4518. GFP_KERNEL);
  4519. if (!child_ctx)
  4520. return -ENOMEM;
  4521. __perf_event_init_context(child_ctx, child);
  4522. child->perf_event_ctxp = child_ctx;
  4523. get_task_struct(child);
  4524. }
  4525. ret = inherit_group(event, parent, parent_ctx,
  4526. child, child_ctx);
  4527. if (ret)
  4528. *inherited_all = 0;
  4529. return ret;
  4530. }
  4531. /*
  4532. * Initialize the perf_event context in task_struct
  4533. */
  4534. int perf_event_init_task(struct task_struct *child)
  4535. {
  4536. struct perf_event_context *child_ctx, *parent_ctx;
  4537. struct perf_event_context *cloned_ctx;
  4538. struct perf_event *event;
  4539. struct task_struct *parent = current;
  4540. int inherited_all = 1;
  4541. int ret = 0;
  4542. child->perf_event_ctxp = NULL;
  4543. mutex_init(&child->perf_event_mutex);
  4544. INIT_LIST_HEAD(&child->perf_event_list);
  4545. if (likely(!parent->perf_event_ctxp))
  4546. return 0;
  4547. /*
  4548. * If the parent's context is a clone, pin it so it won't get
  4549. * swapped under us.
  4550. */
  4551. parent_ctx = perf_pin_task_context(parent);
  4552. /*
  4553. * No need to check if parent_ctx != NULL here; since we saw
  4554. * it non-NULL earlier, the only reason for it to become NULL
  4555. * is if we exit, and since we're currently in the middle of
  4556. * a fork we can't be exiting at the same time.
  4557. */
  4558. /*
  4559. * Lock the parent list. No need to lock the child - not PID
  4560. * hashed yet and not running, so nobody can access it.
  4561. */
  4562. mutex_lock(&parent_ctx->mutex);
  4563. /*
  4564. * We dont have to disable NMIs - we are only looking at
  4565. * the list, not manipulating it:
  4566. */
  4567. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  4568. ret = inherit_task_group(event, parent, parent_ctx, child,
  4569. &inherited_all);
  4570. if (ret)
  4571. break;
  4572. }
  4573. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  4574. ret = inherit_task_group(event, parent, parent_ctx, child,
  4575. &inherited_all);
  4576. if (ret)
  4577. break;
  4578. }
  4579. child_ctx = child->perf_event_ctxp;
  4580. if (child_ctx && inherited_all) {
  4581. /*
  4582. * Mark the child context as a clone of the parent
  4583. * context, or of whatever the parent is a clone of.
  4584. * Note that if the parent is a clone, it could get
  4585. * uncloned at any point, but that doesn't matter
  4586. * because the list of events and the generation
  4587. * count can't have changed since we took the mutex.
  4588. */
  4589. cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
  4590. if (cloned_ctx) {
  4591. child_ctx->parent_ctx = cloned_ctx;
  4592. child_ctx->parent_gen = parent_ctx->parent_gen;
  4593. } else {
  4594. child_ctx->parent_ctx = parent_ctx;
  4595. child_ctx->parent_gen = parent_ctx->generation;
  4596. }
  4597. get_ctx(child_ctx->parent_ctx);
  4598. }
  4599. mutex_unlock(&parent_ctx->mutex);
  4600. perf_unpin_context(parent_ctx);
  4601. return ret;
  4602. }
  4603. static void __init perf_event_init_all_cpus(void)
  4604. {
  4605. int cpu;
  4606. struct perf_cpu_context *cpuctx;
  4607. for_each_possible_cpu(cpu) {
  4608. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4609. mutex_init(&cpuctx->hlist_mutex);
  4610. __perf_event_init_context(&cpuctx->ctx, NULL);
  4611. }
  4612. }
  4613. static void __cpuinit perf_event_init_cpu(int cpu)
  4614. {
  4615. struct perf_cpu_context *cpuctx;
  4616. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4617. spin_lock(&perf_resource_lock);
  4618. cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
  4619. spin_unlock(&perf_resource_lock);
  4620. mutex_lock(&cpuctx->hlist_mutex);
  4621. if (cpuctx->hlist_refcount > 0) {
  4622. struct swevent_hlist *hlist;
  4623. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  4624. WARN_ON_ONCE(!hlist);
  4625. rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
  4626. }
  4627. mutex_unlock(&cpuctx->hlist_mutex);
  4628. }
  4629. #ifdef CONFIG_HOTPLUG_CPU
  4630. static void __perf_event_exit_cpu(void *info)
  4631. {
  4632. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  4633. struct perf_event_context *ctx = &cpuctx->ctx;
  4634. struct perf_event *event, *tmp;
  4635. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  4636. __perf_event_remove_from_context(event);
  4637. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  4638. __perf_event_remove_from_context(event);
  4639. }
  4640. static void perf_event_exit_cpu(int cpu)
  4641. {
  4642. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  4643. struct perf_event_context *ctx = &cpuctx->ctx;
  4644. mutex_lock(&cpuctx->hlist_mutex);
  4645. swevent_hlist_release(cpuctx);
  4646. mutex_unlock(&cpuctx->hlist_mutex);
  4647. mutex_lock(&ctx->mutex);
  4648. smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
  4649. mutex_unlock(&ctx->mutex);
  4650. }
  4651. #else
  4652. static inline void perf_event_exit_cpu(int cpu) { }
  4653. #endif
  4654. static int __cpuinit
  4655. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  4656. {
  4657. unsigned int cpu = (long)hcpu;
  4658. switch (action) {
  4659. case CPU_UP_PREPARE:
  4660. case CPU_UP_PREPARE_FROZEN:
  4661. perf_event_init_cpu(cpu);
  4662. break;
  4663. case CPU_DOWN_PREPARE:
  4664. case CPU_DOWN_PREPARE_FROZEN:
  4665. perf_event_exit_cpu(cpu);
  4666. break;
  4667. default:
  4668. break;
  4669. }
  4670. return NOTIFY_OK;
  4671. }
  4672. /*
  4673. * This has to have a higher priority than migration_notifier in sched.c.
  4674. */
  4675. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  4676. .notifier_call = perf_cpu_notify,
  4677. .priority = 20,
  4678. };
  4679. void __init perf_event_init(void)
  4680. {
  4681. perf_event_init_all_cpus();
  4682. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  4683. (void *)(long)smp_processor_id());
  4684. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
  4685. (void *)(long)smp_processor_id());
  4686. register_cpu_notifier(&perf_cpu_nb);
  4687. }
  4688. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
  4689. struct sysdev_class_attribute *attr,
  4690. char *buf)
  4691. {
  4692. return sprintf(buf, "%d\n", perf_reserved_percpu);
  4693. }
  4694. static ssize_t
  4695. perf_set_reserve_percpu(struct sysdev_class *class,
  4696. struct sysdev_class_attribute *attr,
  4697. const char *buf,
  4698. size_t count)
  4699. {
  4700. struct perf_cpu_context *cpuctx;
  4701. unsigned long val;
  4702. int err, cpu, mpt;
  4703. err = strict_strtoul(buf, 10, &val);
  4704. if (err)
  4705. return err;
  4706. if (val > perf_max_events)
  4707. return -EINVAL;
  4708. spin_lock(&perf_resource_lock);
  4709. perf_reserved_percpu = val;
  4710. for_each_online_cpu(cpu) {
  4711. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4712. raw_spin_lock_irq(&cpuctx->ctx.lock);
  4713. mpt = min(perf_max_events - cpuctx->ctx.nr_events,
  4714. perf_max_events - perf_reserved_percpu);
  4715. cpuctx->max_pertask = mpt;
  4716. raw_spin_unlock_irq(&cpuctx->ctx.lock);
  4717. }
  4718. spin_unlock(&perf_resource_lock);
  4719. return count;
  4720. }
  4721. static ssize_t perf_show_overcommit(struct sysdev_class *class,
  4722. struct sysdev_class_attribute *attr,
  4723. char *buf)
  4724. {
  4725. return sprintf(buf, "%d\n", perf_overcommit);
  4726. }
  4727. static ssize_t
  4728. perf_set_overcommit(struct sysdev_class *class,
  4729. struct sysdev_class_attribute *attr,
  4730. const char *buf, size_t count)
  4731. {
  4732. unsigned long val;
  4733. int err;
  4734. err = strict_strtoul(buf, 10, &val);
  4735. if (err)
  4736. return err;
  4737. if (val > 1)
  4738. return -EINVAL;
  4739. spin_lock(&perf_resource_lock);
  4740. perf_overcommit = val;
  4741. spin_unlock(&perf_resource_lock);
  4742. return count;
  4743. }
  4744. static SYSDEV_CLASS_ATTR(
  4745. reserve_percpu,
  4746. 0644,
  4747. perf_show_reserve_percpu,
  4748. perf_set_reserve_percpu
  4749. );
  4750. static SYSDEV_CLASS_ATTR(
  4751. overcommit,
  4752. 0644,
  4753. perf_show_overcommit,
  4754. perf_set_overcommit
  4755. );
  4756. static struct attribute *perfclass_attrs[] = {
  4757. &attr_reserve_percpu.attr,
  4758. &attr_overcommit.attr,
  4759. NULL
  4760. };
  4761. static struct attribute_group perfclass_attr_group = {
  4762. .attrs = perfclass_attrs,
  4763. .name = "perf_events",
  4764. };
  4765. static int __init perf_event_sysfs_init(void)
  4766. {
  4767. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  4768. &perfclass_attr_group);
  4769. }
  4770. device_initcall(perf_event_sysfs_init);