perf_event.c 129 KB

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