perf_event.c 129 KB

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