perf_event.c 129 KB

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