perf_event.c 123 KB

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