perf_event.c 123 KB

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