perf_event.c 114 KB

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