perf_counter.c 114 KB

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