perf_event.c 120 KB

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