perf_counter.c 113 KB

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