perf_event.c 131 KB

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