perf_event.c 126 KB

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